使用参考列表按名称查找特定文件
Using a reference list to find specific files by name
我有从存储在变量中的 csv 中提取的文件名列表,我想在某个目录中搜索,请参阅下面将此列表提取到变量名文件名。如果找到我想对匹配的文件一个一个地进行一定的操作
#Python 3.5, Pandas 0.17.1
#Reading in list with characteristics of files
index = pd.read_csv('INDEX.csv',usecols = ['Filenumber','Province'])
#Automating extraction of column filename on basis of province and full rows
province = index[index.Province == 'MG']
filenames = province['Filenumber']
到目前为止我还没有找到解决这个问题的办法。我是 Python 的新手,因此非常感谢任何朝着正确方向的推动,无论是以解决方案的形式还是参考 material 来阅读这些类型的操作。
总结:
使用具有文件名称(减去文件类型 (.csv))的变量文件名,我想在一个 directory/folder 中找到具有这些名称的文件。然后(或在搜索文件期间)对每个文件执行特定操作。
这是您要找的吗?
import os
filenames =['testfile1','testfile2']
for file in filenames:
if os.path.isfile(file+'.csv'):
print "File "+ file + ".csv exists."
else:
print "File "+ file + ".csv does not exist."
编辑:以下是 python 3.x 的(未经测试,因为我有 none):
import os
filenames =['testfile1.csv','testfile2.csv']
for file in filenames:
if os.path.isfile(file):
print("File exists.")
else:
print("File does not exist.")
我有从存储在变量中的 csv 中提取的文件名列表,我想在某个目录中搜索,请参阅下面将此列表提取到变量名文件名。如果找到我想对匹配的文件一个一个地进行一定的操作
#Python 3.5, Pandas 0.17.1
#Reading in list with characteristics of files
index = pd.read_csv('INDEX.csv',usecols = ['Filenumber','Province'])
#Automating extraction of column filename on basis of province and full rows
province = index[index.Province == 'MG']
filenames = province['Filenumber']
到目前为止我还没有找到解决这个问题的办法。我是 Python 的新手,因此非常感谢任何朝着正确方向的推动,无论是以解决方案的形式还是参考 material 来阅读这些类型的操作。
总结:
使用具有文件名称(减去文件类型 (.csv))的变量文件名,我想在一个 directory/folder 中找到具有这些名称的文件。然后(或在搜索文件期间)对每个文件执行特定操作。
这是您要找的吗?
import os
filenames =['testfile1','testfile2']
for file in filenames:
if os.path.isfile(file+'.csv'):
print "File "+ file + ".csv exists."
else:
print "File "+ file + ".csv does not exist."
编辑:以下是 python 3.x 的(未经测试,因为我有 none):
import os
filenames =['testfile1.csv','testfile2.csv']
for file in filenames:
if os.path.isfile(file):
print("File exists.")
else:
print("File does not exist.")