how to get name of a file in directory using python
Solution 1
You can use 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
.
To just get the basenames you can use map or a list comp with 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.
Solution 2
os.path
implements some useful functions on pathnames. But it doesn't have access to the contents of the path. For that purpose, you can use 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:
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 :
map(path.basename,glob.iglob(pth+"*.mkv"))
Solution 3
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:
import os
files = os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
mkv_files = [_ for _ in files if _[-4:] == ".mkv"]
print mkv_files
Solution 4
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.
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)
Solution 5
You can use 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.
Xonshiz
Software Developer, Occasional Gamer, Foodie, Blogger, YouTuber, Otaku and a wanna be Cyber Security Expert.
Updated on January 24, 2020Comments
-
Xonshiz over 4 years
There is an mkv file in a folder named "
export
". What I want to do is to make a python script which fetches the file name from that export folder. Let's say the folder is at "C:\Users\UserName\Desktop\New_folder\export
".How do I fetch the name?
I tried using this
os.path.basename
andos.path.splitext
.. well.. didn't work out like I expected. -
Padraic Cunningham almost 9 yearsif you were going to use os.listdir,
if f.endswith(".mkv")
would be sufficient -
Mazdak almost 9 years@PadraicCunningham Yeah there are some ways for this task but as you can see in docs.python.org/2/library/fnmatch.html and also since it used Unix shell-style wildcards I think its more pythonic and faster than other ways! what you think?
-
Padraic Cunningham almost 9 years
map(path.basename,iglob(pth+"*.mkv"))
will be faster and more memory efficient -
Padraic Cunningham almost 9 years.endswith is also faster
-
Mazdak almost 9 years@PadraicCunningham Yeah I this its so If you’re just trying to provide a simple mechanism for allowing wildcards in data processing operations, it’s often a reasonable solution. But for matching the file names
glob
is the proper way. -
BlueBright almost 6 yearsThank you. I'm use your code like this => def list_images(input_dir): return list(map(os.path.abspath, iglob(input_dir + "\\" + "*.jpg")))