我想从结构如下的目录中读取特定文件中的数据:
-Directory
-Subdirectory1
-AGreek.srt
-AEnglish.srt
-Subdirectory2
-BGreek.srt
-BEnglish.srt
-test.pytest.py是我的脚本,我应该只读Directory中的英文文件。这是我现在的剧本:
import os
substring = 'English.srt'
for root, subdirs, files in os.walk('Directory'):
for filename in files:
if substring in filename:
fp = open(os.path.abspath(filename))
# Further Action但是,这给了我以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '/home/coder/Spam/AEnglish.srt'如何解决错误?(P.s.:Spam是Directory和test.py所在的文件夹)
发布于 2018-06-21 07:10:38
您需要使用os.path.join(root,filename)访问该文件。
Ex:
import os
substring = 'English.srt'
for root, subdirs, files in os.walk('Directory'):
for filename in files:
if substring in filename:
fp = open(os.path.join(root,filename))
# Further Actionhttps://stackoverflow.com/questions/50962402
复制相似问题