I want to iterate through some files within a folder and send the path of the files to a list. Then I want to pass this list to a subprocess to execute a bash command:
procfiles = []
os.chdir("/path/to/directory")
for root, dirs, files in os.walk('.'):
for file in files:
if '.mp3' in file:
filename = os.path.join(root, file)
print(filename)
procfiles.append(filename)
print(procfiles)
args = [command, with, arguments].extend(procfiles)
process = subprocess.Popen(args, shell=False)
output, error = process.communicate()
But I get the following output when the file contains a german umlauts letter. For example: ä, ö or ü
./titleWith ä or ü - artist with ü.mp3 #print(filename)
['./titleWith \udcc3\udca4 or \udcc3\udcbc - artist with \udcc3\udcbc.mp3'] #print(procfiles)
This means that there is something wrong with the encoding during the
procfiles.append(filename)
process, right?
After that the subprocess fails with the error:
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcc3' in position 43: surrogates not allowed
Infos:
- Python 3.5.3
- OS: Debian Jessie
- Kernel: 4.9.58+
- architecture: armhf
UPDATE:
I just noticed that when I am executing it manually with the user root or www-data it works, but when I execute it via my custom php script (its only a shell_exec('/usr/bin/python3 /path/to/script.py >> /path/to/log.log 2>&1')
) it doesn't work.
Shouldn't that be the same as when I execute it from the user www-data manually? Or do I have some other environment variables set when the python script is executed from a php script?