简体   繁体   English

Python,我如何在文件夹中找到以特定格式结尾的文件

[英]Python, how do i find files that end with a specific format in a folder

import shutil, os

path = 'C:\\Users\\cHaTrAp\Documents\\My Games\\KillingFloor2\\KFGame\\Cache\\*'

files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.kfm' in file:
            files.append(os.path.join(r, file))

for f in files:
    print(f)

I am trying to find a specific files in a folder that ends with .kfm so i can move them.我试图在以 .kfm 结尾的文件夹中查找特定文件,以便我可以移动它们。 I am having problem to search multiple folder.我在搜索多个文件夹时遇到问题。

you are doing everything fine but your path is wrong.你做的一切都很好,但你的道路是错误的。

path = 'C:\\Users\\cHaTrAp\Documents\\My Games\\KillingFloor2\\KFGame\\Cache\\*'

won't work不会工作

path = 'C:\\Users\\cHaTrAp\Documents\\My Games\\KillingFloor2\\KFGame\\Cache'

the * is not needed. * 不是必需的。

Also like in the comment you should use endswith to avoid files that has '.kfm' in their name instead as file extension.也像在评论中一样,您应该使用 Endswith 来避免名称中包含“.kfm”的文件而不是文件扩展名。

The following will give a you a list of files under the current directory tree, with the specified suffix.下面会给你一个当前目录树下的文件列表,带有指定的后缀。

from pathlib import Path

filelist = list( Path( '.' ).glob('**/*.kfm') )

print( filelist )

In the following, we go a step further.下面,我们更进一步。 We sort the filelist, and then loop over the files我们对文件列表进行排序,然后遍历文件

from pathlib import Path

mysubdir = 'whatever'

pathlist = Path( mysubdir).glob('**/*.kfm')

filelist = sorted( [str(file) for file in pathlist] )

for file in filelist:
    print( file )

here First thing your path is wrong it should be这里首先你的路径是错误的,它应该是

path = 'C:\\Users\\cHaTrAp\Documents\\My Games\\KillingFloor2\\KFGame\\Cache'

then to get files with .kfm extention you can also use below code然后要获取扩展名为 .kfm 的文件,您还可以使用以下代码

import os
files = os.listdir(path)
kfm_files=[f for f in files if '.kfm' in f]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 在特定文件夹中找到今天创建的所有文件 - How do I find all the files that were created today in specific folder using Python 我如何在 python 的特定文件夹中保存.json 文件 - How do i save .json files in specific folder in python 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 如何将具有特定文件扩展名的文件复制到我的python(2.5版)脚本中的文件夹中? - How do I copy files with specific file extension to a folder in my python (version 2.5) script? 如何将文件上传到Django中的特定文件夹? - How do I upload files to a specific folder in Django? 如何创建将在特定时间结束的Python循环? - How do I create a Python loop that will end at a specific time? 如何复制具有特定扩展名的文件夹目录中的文件,然后将其保存到新文件夹中而不覆盖已复制的文件? - How do I copy files in a folder directory with a specific extension and save it to a new folder with out overwriting copied files? 我发现文件结尾为空,python - i find the files at the end empty ,python 如何在特定文件夹上运行 python 脚本 - How do I run python script on specific folder 如何将文件复制到python中的只读文件夹 - How do i copy files to a read-only folder in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM