【问题标题】:Walking directory tree and appending datestamps to file names遍历目录树并将日期戳附加到文件名
【发布时间】:2021-01-21 16:29:58
【问题描述】:

我有一个大约 900 字、excel、PDF 文件的目录,我的最终目标是我只想扫描目录中的 PDF 文档,将它们移动到一个文件中,给它们加上日期戳,然后搜索某些公司名称,返回找到文本的文件名/日期戳。

我编写此代码的第一步是首先通过剥离我不需要的文件/同时复制 PDF 文件来组织我的文件,重命名每个 PDF 文件以在每个文件名中包含创建日期。但是,我正在努力使这些第一个基础知识发挥作用。到目前为止,这是我的代码,位于少数文件的测试目录中 - 到目前为止,我已将其设置为打印每个文件夹、子文件夹和文件名,以检查遍历是否正常工作,这是可行的:

import os
import datetime

os.chdir(r'H:\PyTest')

def modification_date(filename):
    t = os.path.getctime(filename)
    return datetime.datetime.fromtimestamp(t).year, datetime.datetime.fromtimestamp(t).month

#Test function works
modification_date(r'H:\PyTest\2010\Oct\Meeting Minutes.docx')
#output: (2020, 10)
   
#for loop walks through the main folder, each subfolder and each file and prints the name of each pdf file found
for folderName, subfolders, filenames in os.walk('H:\PyTest'):
    print ('the current folder is ' + folderName)
    
    for subfolder in subfolders:
        print('SUBFOLDER OF ' + folderName + ':' + subfolder)
    
    for filename in filenames:
        if filename.endswith('pdf'):
            print(filename)
            #print(modification_date(filename))

没有我注释掉的最后一点,print(modification_date(filename),这似乎可以打印出任何 pdf 的目录和名称。

the current folder is H:\PyTest
SUBFOLDER OF H:\PyTest:2010
SUBFOLDER OF H:\PyTest:2011
SUBFOLDER OF H:\PyTest:2012
the current folder is H:\PyTest\2010
SUBFOLDER OF H:\PyTest\2010:Dec
SUBFOLDER OF H:\PyTest\2010:Oct
the current folder is H:\PyTest\2010\Dec
HF Cheat Sheet.pdf
the current folder is H:\PyTest\2010\Oct
the current folder is H:\PyTest\2011
SUBFOLDER OF H:\PyTest\2011:Dec
SUBFOLDER OF H:\PyTest\2011:Oct
the current folder is H:\PyTest\2011\Dec
HF Cheat Sheet.pdf
the current folder is H:\PyTest\2011\Oct
the current folder is H:\PyTest\2012
SUBFOLDER OF H:\PyTest\2012:Dec
SUBFOLDER OF H:\PyTest\2012:Oct
the current folder is H:\PyTest\2012\Dec
HF Cheat Sheet.pdf
the current folder is H:\PyTest\2012\Oct

但是,由于我的代码中包含 print(modification_date(filename),我收到了 FileNotFound 错误。所以该函数似乎不知道目录路径,这就是它失败的原因。

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'HF Cheat Sheet.pdf'

谁能建议编辑如何获取日期戳,然后更改每个 pdf 名称以将其包含在开头或结尾?我正在查找文件上次保存的日期。

非常感谢

【问题讨论】:

  • os.walk() 返回的filenames 不是完整的路径,并且被假定为相对于当前工作目录——因此您需要将它们中的每一个os.path.join() 指向当前folderName 获取西装参数以传递您的 modification_date() 函数。

标签: python batch-rename datestamp directory-tree


【解决方案1】:

您必须使用 var folderName 构造文件的完整路径。会是这样的:

for folderName, subfolders, filenames in os.walk('H:\PyTest'):
    print ('the current folder is ' + folderName)
    
    for subfolder in subfolders:
        print('SUBFOLDER OF ' + folderName + ':' + subfolder)
    
    for filename in filenames:
        if filename.endswith('pdf'):
            print(filename)
            print(modification_date(os.path.join(folderName,filename)))

folderName(通常这个变量被称为root)中存储的是路径from:你输入os.walk()的路径to :迭代中的当前文件夹。要获取文件的完整路径,您必须将其与文件名连接起来。

【讨论】:

    猜你喜欢
    • 2013-03-04
    • 2014-07-28
    • 2021-03-09
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多