Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Bash - Get filenames with escaped characters

How can I parse filenames with spaces, parentheses etc. into a variable? Ex.

'Album Artist - Song name (feat Musician) [Year]'

to

'Album\ Artist\ \- Song\ name\ \(feat\ Musician\)\ \[Year\]'

I get the right format with re.escape(filename). However if I store the print from re.escape into a variable it gets reversed to the initial naming. I know that I could use the "string".replace('x', 'y') method. But it does not appeal safe to me.

Does anybody know how I can fix this or work around this problem? Using Python 3.5.3 btw.

EDIT example code:

>>> import re 
>>> # this is an example array in the format how my filenames are named stored in files                                                                  >>> files = ['AA - BB (CC) [DD]', 'EE - FF (GG) [HH]', 'II - JJ (KK) [LL]']
>>> for f in files:
...     print(f)
...
AA - BB (CC) [DD]
EE - FF (GG) [HH]
II - JJ (KK) [LL]
>>> for f in files:
...     print(re.escape(f))
...
AA\ \-\ BB\ \(CC\)\ \[DD\] # desired format
EE\ \-\ FF\ \(GG\)\ \[HH\]
II\ \-\ JJ\ \(KK\)\ \[LL\]
>>> escaped = re.escape(files[0])
>>> escaped
'AA\\ \\-\\ BB\\ \\(CC\\)\\ \\[DD\\]' # actual result
>>>
like image 465
valerius21 Avatar asked Dec 22 '17 22:12

valerius21


2 Answers

The underlying problem sounds like passing file names that may contain characters that would need to be escaped as arguments to another program. I suggest looking at subprocess.

Specifically, see frequently used arguments:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

For example:

import subprocess

file_names = [r'AA - BB (CC) [DD]', r'EE - FF (GG) [HH]', r'II - JJ (KK) [LL]']

for file_name in file_names:
    subprocess.call([r'touch', file_name])
like image 139
Galen Avatar answered Oct 29 '22 15:10

Galen


Your variable is ok. Try using print(escaped) not escaped alone.

like image 44
progmatico Avatar answered Oct 29 '22 15:10

progmatico