python string to prevent escape

Transfer part: https://www.cnblogs.com/hellofengying/p/10183057.html

When re-open the file name today, an error occurred, as follows:

In [4]: path='D:\Code\PythonWorkPlace\PythonProject\pydata-book-2nd-edition\data
sets\bitly_usagov\example.txt'

In [5]: open(path).readline()
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
C:\Users\Administrator\<ipython-input-5-bcaecf00da5d> in <module>()
----> 1 open(path).readline()

IOError: [Errno 22] invalid mode ('r') or filename: 'D:\\Code\\PythonWorkPlace\\
PythonProject\\pydata-book-2nd-edition\\datasets\x08itly_usagov\\example.txt'

Not carefully observe really do not notice the file name change (as shown in red font), which is the python escaped string occurred.

 

Solution:

1. string constant, the string before adding r  

str=r“adfdfasd\tfdsadf\t”

print(str)

Results: adfdfasd \ tfdsadf \ t

 

2. The string is read from a file, it is added at the time of reading r

  For example: There is a file test.txt have adfdfasd \ tfdsadf \ t E: \ codes

              Then the code can write:

            file1=open(r"text.txt","r")

            for line in file1:

                   print(line)

         The results are:

                  adfdfasd\tfdsadf\t                   E:\codes

Guess you like

Origin www.cnblogs.com/sggggr/p/11874902.html