Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用绝对路径,FileNotFoundError_Python_Python 3.x_File Io_Python 3.6 - Fatal编程技术网

Python 使用绝对路径,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

这应该是大声读取传递的文件。当我为正在读取的任何文本文件传递一个绝对路径时,我会得到一个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:
      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