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

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

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

me@DESKTOP-3PC51JV:~/temp$ rm *
me@DESKTOP-3PC51JV:~/temp$ echo spam > 'test|file?name1'
me@DESKTOP-3PC51JV:~/temp$ echo eggs > 'test:file<name2'
me@DESKTOP-3PC51JV:~/temp$ ls
'test:file<name2'  'test|file?name1'
me@DESKTOP-3PC51JV:~/temp$ cat 'test|file?name1'
spam

#-----------------------------------------------
# Run WSL's own Python
#-----------------------------------------------

me@DESKTOP-3PC51JV:~/temp$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> sys.platform
'linux'
>>>
>>> os.listdir('.')
['test|file?name1', 'test:file<name2']
>>>
>>> open('test|file?name1').read()
'spam\n'
>>>
>>> open('test<file>name3', 'w').write('toast\n')
6
>>> os.listdir('.')
['test|file?name1', 'test<file>name3', 'test:file<name2']
>>>
>>> os.getcwd()
'/home/me/temp'
>>>

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

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

#-----------------------------------------------
# Run Windows' native Python
#-----------------------------------------------

me@DESKTOP-3PC51JV:~/temp$ /mnt/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\uf07cfile\uf03fname1', 'test\uf03cfile\uf03ename3', 'test\uf03afile\uf03cname2']
>>>
>>> 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()
'\\\\wsl$\\Ubuntu-18.04\\home\\me\\temp'
>>> ^Z



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