gpt4 book ai didi

python - 在python中从文件中读取文件名

转载 作者:行者123 更新时间:2023-11-28 19:54:54 26 4
gpt4 key购买 nike

我正在使用

从 ascii 文件中读取文件名
with open('FilenamesAsciiFile.txt') as f:
content = f.readlines()

不幸的是,当我尝试打开文件或检查它是否存在时,出现错误:

filename = content[0]
print filename
print type(filename)

--> myFile.h5
--> <type 'str'>


import os.path
os.path.isfile(filename)

--> False

当我对字符串进行硬编码时,一切正常

os.path.isfile('myFile.h5')

--> True

字符串是该函数的错误输入类型吗?

最佳答案

您需要从行中删除 '\n':

filename = content[0].strip()

关于python - 在python中从文件中读取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32884259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com