您可以按照以下方式从字符串创建原始字符串:
test_file=open(r'c:\Python27\test.txt','r')
如何从字符串变量中创建一个原始变量,例如:
path = 'c:\Python27\test.txt'
test_file=open(rpath,'r')
因为我有一个文件路径:
file_path = "C:\Users\b_zz\Desktop\my_file"
当我执行:
data_list = open(os.path.expandvars(file_path),"r").readlines()
我得到:Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
scheduled_data_list = open(os.path.expandvars(file_path),"r").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\x08_zz\\Desktop\\my_file'
r"C:\Users\b_zz\Desktop\my_file"
呢?或者更好的方式是"C:/Users/b_zz/Desktop/my_file"
? - Karl Knechtel