您可以通过这种方式从字符串创建原始字符串:
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'