Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 遍历目录树并将日期戳附加到文件名_Python_Batch Rename_Datestamp_Directory Tree - Fatal编程技术网

Python 遍历目录树并将日期戳附加到文件名

Python 遍历目录树并将日期戳附加到文件名,python,batch-rename,datestamp,directory-tree,Python,Batch Rename,Datestamp,Directory Tree,我有一个大约900字、excel、PDF文件的目录,我的最终目标是只扫描目录中的PDF文件,将它们移动到一个文件中,加盖日期戳,然后搜索某些公司名称,返回找到文本的文件名/日期戳 我编写这段代码的第一步是首先整理我的文件,剥离我不需要的内容/复制PDF文件,同时重命名每个PDF文件,在每个文件名中包含创建日期。然而,我正在努力让这些第一基础工作。这是我到目前为止的代码,在一个包含少量文件的测试目录中-到目前为止,我已将其设置为打印每个文件夹、子文件夹和文件名,以检查漫游是否正常工作,这是有效的:

我有一个大约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\u 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
然而,由于代码中包含了打印(修改)日期(文件名),我得到了FileNotFound错误。因此,函数似乎不知道目录路径,这就是它失败的原因

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'HF Cheat Sheet.pdf'
谁能建议编辑如何获得日期戳,然后更改每个pdf名称,使其包含在开始或结束处?我正在查找文件上次保存的日期


非常感谢

您必须使用var
folderName
构建文件的完整路径

对于os.walk('H:\PyTest')中的folderName、子文件夹和文件名:
打印('当前文件夹为'+folderName)
对于子文件夹中的子文件夹:
打印(“+folderName+”的子文件夹:”+子文件夹)
对于文件名中的文件名:
如果filename.endswith('pdf'):
打印(文件名)
打印(修改日期(os.path.join(folderName,filename)))

folderName
(通常该变量称为
root
)中,存储的是来自
的路径:放置在
os.walk()中的路径
:迭代中的当前文件夹。要获取文件的完整路径,必须将其与文件名连接。

os.walk()
返回的
文件名
不是完整路径,被认为是相对于当前工作目录的-因此需要
os.path.join()
将它们中的每一个添加到当前的
folderName
以获取一个suit参数,从而传递
修改日期()
函数。