Python escape character

I struggled with this for many hours, and although I found a solution, I do not like it. Is there a built-in way to solve this problem:

You are on Windows with a variable containing the path. You are trying to open a file with it, but it contains escape characters that you cannot determine before execution.

If you use shutil and do: shutil.copy(file_path, new_file_path)

It works great.

But if you try to use the same path with:

f = open(file_path, encoding="utf8")

This does not work because '\ a' in the path reads as "Bell" = 7

I tried to do all of this, but the only thing I got was the "restoreruct_broken_string" custom function.

  file_path = "F:\ScriptsFilePath\addons\import_test.py" print(sys.getdefaultencoding()) print() print(file_path.replace('\\', r'\\')) print( '%r' % (file_path)) print( r'r"' + "'" + file_path+ "'") print(file_path.encode('unicode-escape')) print(os.path.normpath(file_path)) print(repr(file_path)) print() print(reconstruct_broken_string(file_path)) backslash_map = { '\a': r'\a', '\b': r'\b', '\f': r'\f', '\n': r'\n', '\r': r'\r', '\t': r'\t', '\v': r'\v' } def reconstruct_broken_string(s): for key, value in backslash_map.items(): s = s.replace(key, value) return s 

Here is the listing:

 utf-8 F:\\ScriptsFilePathddons\\import_test.py 'F:\\ScriptsFilePath\x07ddons\\import_test.py' r"'F:\ScriptsFilePathddons\import_test.py' b'F:\\\\ScriptsFilePath\\x07ddons\\\\import_test.py' F:\ScriptsFilePathddons\import_test.py 'F:\\ScriptsFilePath\x07ddons\\import_test.py' F:\ScriptsFilePath\addons\import_test.py 

Is there a built-in way to do this and not this function? Why does it work with "shutil" and not "open"

thanks

+4
source share
4 answers

Your problem in this line:

 file_path = "F:\ScriptsFilePath\addons\import_test.py" 

Try one of the following:

 file_path = r"F:\ScriptsFilePath\addons\import_test.py" file_path = "F:\\ScriptsFilePath\\addons\\import_test.py" 

Or even:

 file_path = "F:/ScriptsFilePath/addons/import_test.py" 

(Yes, Windows accepts a slash as a file delimiter.)

Link: http://docs.python.org/2/reference/lexical_analysis.html#string-literals

+7
source

Here is a simplified version demonstrating how "repr" does not work properly.

 file_path = "F:\tab\a_bell\newline.py" print(file_path) print(repr(file_path)) 

Fingerprints:

 F: ab_bell ewline.py and F:\tab\x07_bell\newline.py' 

As you can see, "repr" works with the escape-tab, escape-new line, etc., but does not work with "\ a", which is an escape spike.

Is this a mistake in 'repr'? Is there a built-in solution to this problem that does not require programmers to write a custom function, such as "restoreruct_broken_string (s)"? If not, how can python be so lame?

+1
source

If the path is in a variable, just replace all '\' with '/' using any of the Python string management functions. This should solve the problem.

0
source

I had the same problem - try path = 'C: \ temp \ importfile.xlsx' and continued to receive the error message "There is no such file or directory:" C: \ Temp \ importdata.xlsx ". Instead, I used slashes and my import worked. Have you tried file_path = "F: /ScriptsFilePath/addons/import_test.py"?

0
source

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


All Articles