如何在文件名中导入带空格的.txt

时间:2018-04-05 10:46:48

标签: python-3.x

代码:

import sys
with open(sys.argv[1]) as f:
    lines = f.readlines()
for line in lines[0:]:
    columns = line.split()
    print(columns[0])

输出:

Traceback (most recent call last):
    File "scissors.py", line 2, in <module>
        with open (sys.argv[1]) as f:
    FileNotFoundError: [Errno 2] No such file or directory: '1'

(指向旧图片的链接:https://i.stack.imgur.com/aG1vr.pnghttps://i.stack.imgur.com/wSpLG.png

1 个答案:

答案 0 :(得分:0)

  1. 将文件名包装在引号中,如"your file.txt"
  2. 如果您有文件 path ,请在引号前放置r,使用原始字符串(以及复制的文件路径中的反斜杠): r"C:\Folder\Subfolder\another_one\your_file.txt"