使用绝对路径,FileNotFoundError

时间:2017-03-17 20:16:50

标签: 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:
      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 '

1 个答案:

答案 0 :(得分:1)

你的末尾有一个额外的空间。当您输入数据或使用以下命令更改开放行时删除它:

with open(input("Input File Path: ").strip()) as fin: