logo
down
shadow

Unicode filename to python subprocess.call()


Unicode filename to python subprocess.call()

By : flares
Date : January 02 2021, 06:48 AM
wish help you to fix your issue I found a fine workaround, it's a bit messy, but it works.
subprocess.call is going to pass the text in its own encoding to the terminal, which might or not be the one it's expecting. Because you want to make it portable, you'll need to know the machine's encoding at runtime.
code :


Share : facebook icon twitter icon
Python: How to move a file with unicode filename to a unicode folder

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?

How to provide input to a python subprocess call that expects filename, rather than variable?


By : ANproCUBE
Date : March 29 2020, 07:55 AM
hope this fix your issue I suggest to use a named pipe:
code :
import os, tempfile, shutil, subprocess

temp_dir = tempfile.mkdtemp()
filename = os.path.join(temp_dir, 'file.txt')
text = '<text>'
os.mkfifo(filename)

try:
    subprocess.Popen(('segment.sh', 'ctf', filename, 'utf-8', '0'))
    with open(filename, 'w') as f:
        f.write(text)
finally:
    shutil.rmtree(temp_dir)
Python unicode issue with subprocess.call

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

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

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
Related Posts Related Posts :
  • google app engine error ,and i can't open it now.(python)
  • IIS Not Linking to Django with PyISAPIe
  • Create two separate windows in terminal
  • Killing a subprocess including its children from python
  • google search rankings and trends api
  • Easiest way to automatically download required modules in Python?
  • How to read a string one letter at a time in python
  • Python pixel manipulation library
  • python cx_Freeze egg problem
  • How can I select all rows with sqlalchemy?
  • Converting IPv4 or IPv6 address to a long for comparisons
  • Converting BMP to Grey Scale in Image Module Python
  • Python: Memory usage and optimization when modifying lists
  • Mod_wsgi versus fapws3 - Django
  • In Django, how to create tables from an SQL file when syncdb is run
  • What's the pythonic way to use getters and setters?
  • python regular expression for domain names
  • Pickling a class definition
  • How to catch login failures with PySVN?
  • Python: how to enclose strings in a list with < and >
  • Python: finding lowest integer
  • efficiently finding the interval with non-zeros in scipy/numpy in Python?
  • How to convert Selenese (html) to Python programmatically?
  • fade out in Image module Python
  • local variable 'sresult' referenced before assignment
  • how to transfer a python object between two requests?
  • Getting weird python error when I run a simple django script in Eclipse, not happening in console
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk