在 Python 中查找目录中的特定路径 [英] Finding particular path in directory in Python

查看:53
本文介绍了在 Python 中查找目录中的特定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能找到包含像 20170423 这样的日期的目录的路径?意思是,它可以是任何日期,但我想有那个特定的路径,直到我得到一个包含日期的文件夹......沿途可能有几个包含该模式的子目录,但这是一个特殊情况,我需要更精确,比如文件夹的内容来选择合适的.如果您喜欢特殊情况,请试一试,但对于我知道一路上只有一个文件夹包含该模式的情况,我开始的方式是:

 directPaths_list = [f.path for f in os.scandir( start ) if f.is_dir() ]

这给了我从开始文件夹到结束的所有路径.现在,我想返回包含日期模式的路径,特别是一个,让我们说谁的 int(folder_name) 是 <日期时间.日期时间.今天()

我可以想出一个粗略的解决方案,但是 SO 的人在想出简洁优雅的解决方案方面具有非常好的技能,所以它......有什么想法吗?谢谢!

例如,对于路径 start= C:/这将返回 C:\Users\abc\def\ghi\20170412

我在想这样的事情可以工作:

[f.path for f in os.scandir('C:\\Users\\abc\\def\\ghi\\') 如果 f.is_dir() 和 str(f.path).结束(str(2),-2,-1)]

解决方案

假设日期 20170423 是一个相应命名的文件.然后你可以使用 os.walk():

start = "C:\\Users\\abc\\"对于 os.walk(start) 中的目录路径、目录名、文件名:对于文件名中的文件名:如果文件名==20170412":文件名 = os.path.join(目录路径,文件名)打印(文件名)打印(目录路径)

如果只有一个文件存在 C:\Users\abc\def\ghi\20170412 那么上面会输出:

C:\Users\abc\def\ghi\20170412C:\用户\abc\def\ghi\

您当然可以更改 if 语句以适合您是否要检查 filename.startswith("2017") 或其他任何内容.

警告:如果您执行 start = "C:\\" 那么这很可能需要 很长时间才能完成.因为它将遍历 C 驱动器上的每个目录.

您同样可以更改文件名中的文件名

用于目录名中的目录名:如果目录名==20170412":dirname = os.path.join(dirpath, dirname )打印(目录名)打印(目录路径)

因此如果 C:\Users\abc\def\ghi\20170412 实际上是一个目录,那么上面的输出和之前一样:

C:\Users\abc\def\ghi\20170412C:\用户\abc\def\ghi\

<块引用>

我希望有更简洁的东西

如果你称之为更简短的话,你仍然可以把它打包成列表理解.

files = [os.path.join(dir, f) for dir, dirs, files in os.walk(start) for f in files if f == "20170412"]

How could I find the path of the directory which contains a date like 20170423 ? meaning, it could be any date, but i want to have that specific path until i get a folder that contains a date.. There may be several sub-directories along the way that contain that pattern, but that's a special case, where i would need to give more precision, like the content of the folder to select the proper one. give it a shot if you d like for the special case, but for the case where i know that only one folder along the way contains that pattern, the way i start is :

 directPaths_list  =  [f.path for f in os.scandir( start ) if f.is_dir()   ] 

This gives me all the paths from the start folder, to the end. Now, i would like to return the paths that have the date pattern in them, and in particular one, that has let's say whose int(folder_name) is < datetime.datetime.today()

I could come up with a rough solution, but SO has people with very good skills at coming up with succint elegant solutions, so there it is.. any idea? thanks!

for example, for the path start= C:/ this would return C:\Users\abc\def\ghi\20170412

I was thinking something like this could work:

[f.path for f in os.scandir('C:\\Users\\abc\\def\\ghi\\') if f.is_dir() and str(f.path).endswith(str(2),-2,-1) ] 

解决方案

Assuming the date 20170423 is a file named accordingly. Then you could use os.walk():

start = "C:\\Users\\abc\\"

for dirpath, dirnames, filenames in os.walk(start):
    for filename in filenames:
        if filename == "20170412":
            filename = os.path.join(dirpath, filename)
            print(filename)
            print(dirpath)

If only a single file exists C:\Users\abc\def\ghi\20170412 then the above would output:

C:\Users\abc\def\ghi\20170412
C:\Users\abc\def\ghi\

You can of course change the if statement to fit whether you want to check filename.startswith("2017") or anything else.

Warning: If you do start = "C:\\" then this is most likely gonna take a looong time to finish. Because it is going to run through every single directory on the C drive.

You can equally change for filename in filenames

for dirname in dirnames:
    if dirname == "20170412":
        dirname = os.path.join(dirpath, dirname )
        print(dirname)
        print(dirpath)

Thus if C:\Users\abc\def\ghi\20170412 is actually a directory, then the above would output the same as before:

C:\Users\abc\def\ghi\20170412
C:\Users\abc\def\ghi\

I was hoping for something more succinct

You could still pack it into a list comprehension if that's what you call more brief.

files = [os.path.join(dir, f) for dir, dirs, files in os.walk(start) for f in files if f == "20170412"]

这篇关于在 Python 中查找目录中的特定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆