使用绝对路径,FileNotFoundError

Using absolute path, FileNotFoundError

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

你在它的末尾有一个额外的 space。在输入数据时将其删除或更改开放行:

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