Replace parentheses to regex them

I am trying to copy a file,

>>> originalFile = '/Users/alvinspivey/Documents/workspace/Image_PCA/spectra_text/HIS/jean paul test 1 - Copy (2)/bean-1-aa.txt'
>>> copyFile = os.system('cp '+originalFile+' '+NewTmpFile)

But first, you need to replace the spaces and brackets before the public function will work:

/ Users / alvinspivey / Documents / workspace / Image_PCA / spectra_text / HIS / jean \ paul \ test \ 1 \ - \ Copy \\ (2 \) / bean -1-aa.txt

spaces' '→' \ 'parenthesis "(' -> '\ (' etc.

Replacing work spaces:

>>> originalFile = re.sub(r'\s',r'\ ', os.path.join(root,file))

but the brackets return an error:

>>> originalFile = re.sub(r'(',r'\(', originalFile)

Traceback ( ): ", 1, " /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py ", 151,   return _compile (pattern, flags).sub(repl, string, count) " /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", 244, _compile    , v # sre_constants.error:

?

, re.escape() . , .

+2
4

( (), :

originalFile = re.sub(r'\(',r'\(', originalFile)

, regex :

originalFile = re.sub(r'\(','\(', originalFile)
+2

r'(' . Python .

, , , , string.replace ?

+2

, (os.system) , ,

import shutil

originalFile = '/Users/alvinspivey/Documents/workspace/Image_PCA/spectra_text/HIS/jean paul test 1 - Copy (2)/bean-1-aa.txt'
newTmpFile = '/whatever.txt'
shutil.copy(originalFile, newTmpFile)
+2
  • shutil.copy , .
  • , os.system - , .
0

Source: https://habr.com/ru/post/1527093/


All Articles