我有一个带有一组文件名的csv文件,我想检查它们是否存在于目录及其子目录中
CSV格式的文件列表:
List of files
0 add_even_blank_page_with_text.py
1 add_even_page.py
2 add_text_pdf.py
3 waste_data_cleaning.py
4 hello.py
5 111.py
我写了一个有效的脚本,见下文:
#Import Packages
import os
import pandas as pd
import csv
path=r'C:\Users\sarah\.spyder-py3'
file=r'C:\Users\sarah\.spyder-py3\list.csv'
new=r'C:\Users\sarah\.spyder-py3\output90.csv'
#Read in CSV File
list=pd.read_csv(file, header=None,skiprows=[0], dtype=str, names=['File'],usecols=[0], squeeze=True)
print(list)
# Create a workbook and add a worksheet.
f=open(new, 'w', newline='')
writer = csv.writer(f)
#Check if each file exists or not
for root, dirs, files in os.walk(path):
for files in list:
dir=os.path.join(root, files)
if os.path.exists(dir):
print(dir,'- exists')
exists=dir+' -exists'
writer.writerow([exists])
else:
print(dir,'- not exists')
notexists=dir+'not exists'
writer.writerow([notexists])
#Ouput results to csv
#file.close()
但是,输出会列出文件夹和目录中文件夹+文件的完整路径,并说明该文件是否存在。因此,我的excel文件中有100+行
C:\Users\sarah\.spyder-py3\add_even_blank_page_with_text.py - exists
C:\Users\sarah\.spyder-py3\add_even_page.py - exists
C:\Users\sarah\.spyder-py3\add_text_pdf.py - exists
C:\Users\sarah\.spyder-py3\waste_data_cleaning.py - exists
C:\Users\sarah\.spyder-py3\hello.py - not exists
C:\Users\sarah\.spyder-py3\111.py - not exists
C:\Users\sarah\.spyder-py3\.pylint.d\add_even_blank_page_with_text.py - not exists
C:\Users\sarah\.spyder-py3\.pylint.d\add_even_page.py - not exists
C:\Users\sarah\.spyder-py3\.pylint.d\add_text_pdf.py - not exists
C:\Users\sarah\.spyder-py3\.pylint.d\waste_data_cleaning.py - not exists
然而,我想格式化列表,使其列出文件名,其相应的路径,将带回只有5行
add_even_blank_page_with_text.py <FullFilepath> exist
add_even_page.py <FullFilepath> exist
add_text_pdf.py <FullFilepath> exist
waste_data_cleaning.py <FullFilepath> exist
hello.py Not exist
111.py Not exist
有没有人能帮我格式化?在这件事上我似乎绕了一圈。提前谢谢
您可以使用
os.path.basename(<full_file_name_here>)
函数获取基本文件名。将它们保存到列表中,并在打印/保存之前对它们进行排序其次,您可以读取文件夹中的所有文件,然后检查文件是否存在。原因是
os.path.isdir
是IO操作,如果该目录中没有太多的文件(比如几百个文件),成本会更高据我所知,你想知道你是否看过每个文件。您可以使用一个集合来完成此操作,然后在末尾打印它。下面是一个基本示例,它跟踪第一次出现的内容,没有格式或任何花哨的内容:
输出应如下所示:
顺便说一句,避免使用像
list
这样的变量名,因为它会隐藏内置的list
。同样,您在内部循环中覆盖files
相关问题 更多 >
编程相关推荐