Python 使用绝对路径,FileNotFoundError
这应该是大声读取传递的文件。当我为正在读取的任何文本文件传递一个绝对路径时,我会得到一个FileNotFoundError。适用于本地文件名。有什么想法吗Python 使用绝对路径,FileNotFoundError,python,python-3.x,file-io,python-3.6,Python,Python 3.x,File Io,Python 3.6,这应该是大声读取传递的文件。当我为正在读取的任何文本文件传递一个绝对路径时,我会得到一个FileNotFoundError。适用于本地文件名。有什么想法吗 #!/usr/bin/env python3 from os import system def text_to_speech(word): system('say %s' % word) with open(input("Input File Path: ")) as fin: for line in fin: te
#!/usr/bin/env python3
from os import system
def text_to_speech(word):
system('say %s' % word)
with open(input("Input File Path: ")) as fin:
for line in fin:
text_to_speech(line)
这是堆栈:
Traceback (most recent call last):
File "timer.py", line 7, in <module>
with open(input("Input File Path: ")) as fin:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/codeboy/Desktop/Project/test.txt '
回溯(最近一次呼叫最后一次):
文件“timer.py”,第7行,在
打开(输入(“输入文件路径:”)作为fin:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/Users/codeboy/Desktop/Project/test.txt'
它的末尾有一个额外的空间。在输入数据时删除该选项,或使用以下命令更改打开的行:
with open(input("Input File Path: ").strip()) as fin:
可能是文件路径末尾的空格吗?哦,天哪,facepalm