列出给定目录及其子目录中存在和不存在的文件

2024-04-20 02:59:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有一组文件名的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

有没有人能帮我格式化?在这件事上我似乎绕了一圈。提前谢谢


Tags: 文件csvtextpyadddirexistspage
2条回答

您可以使用os.path.basename(<full_file_name_here>)函数获取基本文件名。将它们保存到列表中,并在打印/保存之前对它们进行排序

其次,您可以读取文件夹中的所有文件,然后检查文件是否存在。原因是os.path.isdir是IO操作,如果该目录中没有太多的文件(比如几百个文件),成本会更高

据我所知,你想知道你是否看过每个文件。您可以使用一个集合来完成此操作,然后在末尾打印它。下面是一个基本示例,它跟踪第一次出现的内容,没有格式或任何花哨的内容:

import os

path = r'C:\Users\sarah\.spyder-py3'

to_find = {
    'add_even_blank_page_with_text.py',
    'add_even_page.py',
    'add_text_pdf.py',
    'waste_data_cleaning.py',
    'hello.py',
    '111.py',
    }

found = set()

for root, dirs, files in os.walk(path):
    if not to_find:  # If none left to find, stop looking
        break

    # Files we're searching for that are in the current "root"
    for file in to_find & set(files):
        found.add((root, file))
        to_find.remove(file)

for root, file in found:
    print('+', os.path.join(root, file))

for file in to_find:
    print('-', file)

输出应如下所示:

+ C:\Users\sarah\.spyder-py3\add_even_blank_page_with_text.py
+ C:\Users\sarah\.spyder-py3\add_even_page.py
+ C:\Users\sarah\.spyder-py3\add_text_pdf.py
+ C:\Users\sarah\.spyder-py3\waste_data_cleaning.py
- hello.py
- 111.py

顺便说一句,避免使用像list这样的变量名,因为它会隐藏内置的list。同样,您在内部循环中覆盖files

相关问题 更多 >