Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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_Pathlib - Fatal编程技术网

在Python中查找已更改的子目录中的文件

在Python中查找已更改的子目录中的文件,python,pathlib,Python,Pathlib,我有一个文件名的文本文件。比如: C:\Folder\Subfolder_01\file_1001.csv C:\Folder\Subfolder_02\file_3030.xls ... 我想检查文件是否仍然存在(这很容易),或者子文件夹的名称是否已更改。某些子文件夹的名称通过在其前面添加一些字符串而更改(以4位数字开头,例如C:\Folder\Subfolder\u 02\file\u 3030.xls已更改为C:\Folder\2019-Subfolder\u 02\file\u 303

我有一个文件名的文本文件。比如:

C:\Folder\Subfolder_01\file_1001.csv
C:\Folder\Subfolder_02\file_3030.xls
...
我想检查文件是否仍然存在(这很容易),或者子文件夹的名称是否已更改。某些子文件夹的名称通过在其前面添加一些字符串而更改(以4位数字开头,例如
C:\Folder\Subfolder\u 02\file\u 3030.xls
已更改为
C:\Folder\2019-Subfolder\u 02\file\u 3030.xls

我试图用
pathlib.glob()
解决这个问题。可以“手动”为一个特定文件执行此操作,如

list(file.parent.parent.glob('* - Subfolder_02\file_3030.xls'))
它返回一个具有新文件名的列表。但是我在一个围绕着
glob
的循环中没有做到这一点

这就是我目前所得到的结果,但我尝试将glob与其他变量连接(使用+)失败的原因很明显:

import pathlib

file = pathlib.Path(file_names.txt)
lines=[]

with open(file,'r') as f:
    # reading the txt-file line by line         
    for line in f:
        line = line.replace("\r", "").replace("\n", "")
        lines.append(line)

for file in lines:
    file = pathlib.Path(file)
    # check if file exists ...
    if file.exists():
        print('OK - ' + file.name)
    # ... if not, find new location
    else:
        new_files = list(file.parent.parent.glob('* - ') + file.name)
        print(files_files)  

我会将您的顶级目录设置为路径,如果您在其原始位置找不到该文件,则使用该路径对该目录下的文件进行全局搜索。使用glob中的
**
将搜索所有文件夹

# Set top level directory as desired.
parent_dir = Path('.')

# you can use splitlines() to parse the file into a list
with Path('file_names.txt').open() as f:
    files = f.read().splitlines()

for f in files:
    orig = Path(f)

    # Still in location, no need to look further
    if orig.exists():
        print(f"{orig.absolute()} is still in place.")
        continue

    # See if we can find it under parent_dir
    matches = [*parent_dir.glob(f"**/{orig.name}")]

    if len(matches) > 1:
        print("Multiple Matches Found")

    for match in matches:
        print(f"{orig.absolute()} might be in {match.absolute()}")
试一试

例如:

导入操作系统
从watchdog.Observer导入观察者
从watchdog.events导入文件系统EventHandler
资源\u PATH=“C:\Folder”
类目录监视程序(FileSystemEventHandler):
定义初始化(自):
自我观察
self.cur\u dirs=os.listdir(资源路径)
def观察(自我):
self.observer=observer()
self.my_watch=self.observer.schedule(self,path=RESOURCES\u path,recursive=True)
self.observer.start()
def on_已修改(自身,事件=无):
#已修改文件夹:
self.new\u dirs=os.listdir(资源路径)
旧=设置(自身当前目录)-设置(自身新目录)
新建=设置(self.new\u dirs)-设置(self.cur\u dirs)
打印(“{}更改为{}”。格式(旧、新))
self.cur_dirs=self.new_dirs#update cur_dirs

子目录更改时,将触发“修改”上的
,您可以通过保留子目录列表来提取更改的文件夹名称

谢谢您的工作!使用f字串的想法很有魅力。出于我的目的,我将它稍微修改为
matches=[*file.parent.parent.glob(f“*{file.parts[-2]}/{file.name}”)]