簡體   English   中英

如何從python shell打開一個Windows文件

[英]How to open a windows file from a python shell

我是python的新手。 我已經嘗試了教程和教科書中可用的所有技術,通過python shell打開一個已經存在的文件,它總是返回一種形式的錯誤或另一種形式。

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

復制評論

 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 \\

如果打開你的意思是加載一個文件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM