简体   繁体   English

如何使用python获取目录中的文件名

[英]how to get name of a file in directory using python

There is an mkv file in a folder named " export ".在名为“ export ”的文件夹中有一个 mkv 文件。 What I want to do is to make a python script which fetches the file name from that export folder.我想要做的是制作一个 python 脚本,从该导出文件夹中获取文件名。 Let's say the folder is at " C:\\Users\\UserName\\Desktop\\New_folder\\export ".假设该文件夹位于“ C:\\Users\\UserName\\Desktop\\New_folder\\export ”。

How do I fetch the name?我如何获取名称?

I tried using this os.path.basename and os.path.splitext .. well.. didn't work out like I expected.我尝试使用这个os.path.basenameos.path.splitext .. 好吧.. 没有像我预期的那样工作。

os.path implements some useful functions on pathnames. os.path在路径名上实现了一些有用的功能。 But it doesn't have access to the contents of the path.但它无权访问路径的内容。 For that purpose, you can use os.listdir .为此,您可以使用os.listdir

The following command will give you a list of the contents of the given path:以下命令将为您提供给定路径的内容列表:

os.listdir("C:\Users\UserName\Desktop\New_folder\export")

Now, if you just want .mkv files you can use fnmatch ( This module provides support for Unix shell-style wildcards ) module to get your expected file names:现在,如果您只想要.mkv文件,您可以使用fnmatch此模块提供对 Unix shell 样式通配符的支持)模块来获取您预期的文件名:

import fnmatch
import os

print([f for f in os.listdir("C:\Users\UserName\Desktop\New_folder\export") if fnmatch.fnmatch(f, '*.mkv')])

Also as @Padraic Cunningham mentioned as a more pythonic way for dealing with file names you can use glob module :同样,正如@Padraic Cunningham 提到的,作为处理文件名的更pythonic 的方式,您可以使用glob模块:

map(path.basename,glob.iglob(pth+"*.mkv"))

You can use glob :您可以使用glob

from glob import glob

pth ="C:/Users/UserName/Desktop/New_folder/export/"
print(glob(pth+"*.mkv"))

path+"*.mkv" will match all the files ending with .mkv . path+"*.mkv"将匹配所有以.mkv结尾的文件。

To just get the basenames you can use map or a list comp with iglob:要获得基本名称,您可以使用 map 或带有 iglob 的列表组合:

from glob import iglob

print(list(map(path.basename,iglob(pth+"*.mkv"))))


print([path.basename(f) for f in  iglob(pth+"*.mkv")])

iglob returns an iterator so you don't build a list for no reason. iglob 返回一个迭代器,因此您不会无缘无故地构建列表。

I assume you're basically asking how to list files in a given directory.我假设您基本上是在问如何列出给定目录中的文件。 What you want is:你想要的是:

import os
print os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")

If there's multiple files and you want the one(s) that have a .mkv end you could do:如果有多个文件并且您想要具有 .mkv 结尾的文件,您可以执行以下操作:

import os
files = os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
mkv_files = [_ for _ in files if _[-4:] == ".mkv"]
print mkv_files

If you are searching for recursive folder search, this method will help you to get filename using os.walk , also you can get those file's path and directory using this below code.如果您正在搜索递归文件夹搜索,此方法将帮助您使用os.walk获取文件名,您也可以使用以下代码获取这些文件的路径和目录。

import os, fnmatch
for path, dirs, files in os.walk(os.path.abspath(r"C:/Users/UserName/Desktop/New_folder/export/")):
    for filename in fnmatch.filter(files, "*.mkv"):
        print(filename)

You can use glob你可以使用 glob

import glob
for file in glob.glob('C:\Users\UserName\Desktop\New_folder\export\*.mkv'):
    print(str(file).split('\')[-1])

This will list out all the files having extention .mkv as file.mkv, file2.mkv and so on.这将列出所有扩展file.mkv, file2.mkv .mkv 的文件,如file.mkv, file2.mkv等。

From os.walk you can read file paths as a listos.walk您可以读取文件路径作为列表

files = [ file_path  for _, _, file_path in os.walk(DIRECTORY_PATH)]
for file_name in files[0]: #note that it has list of lists
    print(file_name)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM