Python: How to move a file with unicode filename to a unicode folder
By : user2910068
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The basic problem is the unconverted mix between Unicode and byte strings. The solutions can be converting to a single format or avoiding the problems using some trickery. All of my solutions include the glob and shutil standard library. For the sake of example, I have some Unicode filenames ending with ods, and I want to move them to the subdirectory called א (Hebrew Aleph, a unicode character). code :
>>> import glob
>>> import shutil
>>> files=glob.glob('*.ods') # List of Byte string file names
>>> for file in files:
... shutil.copy2(file, 'א') # Byte string directory name
...
>>> import glob
>>> import shutil
>>> files=glob.glob(u'*.ods') # List of Unicode file names
>>> for file in files:
... shutil.copy2(file, u'א') # Unicode directory name
# -*- coding: utf-8 -*-
import os
import shutil
import glob
os.chdir('א') # CD to the destination Unicode directory
print os.getcwd() # DEBUG: Make sure you're in the right place
files=glob.glob('../*.ods') # List of Byte string file names
for file in files:
shutil.copy2(file, '.') # Copy each file
# Don't forget to go back to the original directory here, if it matters
>>> files=glob.glob('*.ods')
>>> for file in files:
... shutil.copy2(file, u'א')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.6/shutil.py", line 98, in copy2
dst = os.path.join(dst, os.path.basename(src))
File "/usr/lib/python2.6/posixpath.py", line 70, in join
path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 1:
ordinal not in range(128)
|
How to provide input to a python subprocess call that expects filename, rather than variable?
By : ANproCUBE
Date : March 29 2020, 07:55 AM
|
Python unicode issue with subprocess.call
By : Kees Boogaard
Date : March 29 2020, 07:55 AM
Any of those help After writing down the question, the cause of the issued to be clear :) \xdf is UTF-8 \xc3\x9fis ISO-8859-1 or latin-1 code :
out_enc = 'latin-1'
engine_parameter = [arg.encode(out_enc) if isinstance(arg, unicode) else arg for arg in engine_parameter]
call(engine_parameter)
|
Python subprocess stdin=subprocess.PIPE and unicode
By : Paulo Graça
Date : March 29 2020, 07:55 AM
With these it helps OK... I will halt the lynch mob on subprocess now. This was completely my bad in being careless with mixing unicode and str types in python 2. When passing a list to the check_call() command it appears as though there is some functionality to encode all unicode before making the command to the os. when using communicate() it expects a single string, but passing a mix of unicode and str types in a list to the str type .join operation it was relying on pythons 'helpful' combining operations which default to encoding and decoding using 'ascii' as the codec. When I changed my code to ensure that everything was unicode in the list and then encoded it as I passed it to communicate things worked as expected. After ensuring my script, password and backup_file variables are of unicode type, my code now looks like: code :
cmd = [u'python', script, u'restore', password, backup_file, u'user']
proc = subprocess.Popen(['at', 'now'], stdin=subprocess.PIPE)
proc.communicate(u' '.join(cmd).encode('utf-8'))
|
Python subprocess.call - adding a variable to subprocess.call
By : Mikail
Date : March 29 2020, 07:55 AM
With these it helps The subprocess.call method taks a list of parameters not a string with space separators unless you tell it to use the shell which is not recommended if the string can contain anything from user input. The best way is to build the command as a list
|