簡體   English   中英

使用絕對路徑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 '

末尾有多余的空間。 輸入數據時將其刪除,或使用以下方式更改打開的行:

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

暫無
暫無

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

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