File: android-deltas-sync/_etc/examples/windows-pc-example/x-cygwin-and-wsl-nonportables/cygwin-nonportables.txt

Demo how Cygwin allows nonportable filenames with Unicode munge
characters, whose interpretation differs between native and Cygwin 
Pythons.  Native Windows ('win32') doesn't unmunge nonportables,
but Cygwin Python ('cygwin') does, via its Cygwin C library.
Thus, name fixing can be skipped for native Windows contexts,
but should still be run for Cygwin to avoid issues. 

#--------------------------------------------------
# Create nonportable filenames in Cygwin on Windows
#--------------------------------------------------

lutz@DESKTOP-3PC51JV ~/temp
$ rm *

lutz@DESKTOP-3PC51JV ~/temp
$ echo spam > 'test|file?name1'

lutz@DESKTOP-3PC51JV ~/temp
$ echo eggs > 'test:file<name2'

lutz@DESKTOP-3PC51JV ~/temp
$ ls
'test:file<name2'  'test|file?name1'

lutz@DESKTOP-3PC51JV ~/temp
$ cat 'test|file?name1'
spam

#--------------------------------------------------
# Run Cygwin's own Python
#--------------------------------------------------

lutz@DESKTOP-3PC51JV ~/temp
$ python
Python 3.6.13 (default, May  5 2021, 13:10:10)
[GCC 10.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> sys.platform
'cygwin'
>>>
>>> os.listdir('.')
['test:file<name2', 'test|file?name1']
>>>
>>> open('test|file?name1').read()
'spam\n'
>>>
>>> open('test<file>name3', 'w').write('toast\n')
6
>>> os.listdir('.')
['test:file<name2', 'test<file>name3', 'test|file?name1']
>>>
>>> os.getcwd()
'/home/lutz/temp'
>>>

#--------------------------------------------------
# Back to the Bash shell
#--------------------------------------------------

lutz@DESKTOP-3PC51JV ~/temp
$ ls
'test:file<name2'  'test<file>name3'  'test|file?name1'

lutz@DESKTOP-3PC51JV ~/temp
$ cat 'test<file>name3'
toast

#--------------------------------------------------
# Run Windows' native Python
#--------------------------------------------------
 
lutz@DESKTOP-3PC51JV ~/temp
$ /cygdrive/c/Python/python.exe
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> sys.platform
'win32'
>>>
>>> os.listdir('.')
['test\uf03afile\uf03cname2', 'test\uf03cfile\uf03ename3', 'test\uf07cfile\uf03fname1']
>>>
>>> open('test|file?name1').read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'test|file?name1'
>>>
>>> open('test<file>name4', 'w').write('hmm')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'test<file>name4'
>>>
>>> open('test\uf03afile\uf03cname2').read()
'eggs\n'
>>>
>>> os.getcwd()
'C:\\cygwin64\\home\\lutz\\temp'
>>> ^Z




[Home page] Books Code Blog Python Author Train Find ©M.Lutz