简体   繁体   中英

How to open a windows file from a python shell

I am new to python. i have tried all the techniques available in tutorials and textbooks to open an already existing file through the python shell, it always return one form of error or the other.

test_file = open('C:\Users\User\Documents\test.txt')

Copy comment :

 Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> see_file = open('Arrows') FileNotFoundError: [Errno 2] No such file or directory: 'Arrows' test_file = open('C:\\Users\\User\\Documents\\test.txt') SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape 
file_open = open('C:\\Users\\User\\Documents\\test.txt')

这应该做的伎俩,你需要转义特殊字符,如\\ with \\

If by opening you mean loading a file:

path = "C:/Users/User/Documents/test.txt"
file = open(path, 'r')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM