12

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 and os.path.splitext .. well.. didn't work out like I expected.

2
  • 1
    Why didn't os.path.basename work? Can you show an example code in which it didn't work? Commented Jul 4, 2015 at 15:12
  • Can you paste your code. Commented Jul 4, 2015 at 15:13

6 Answers 6

14

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"))
Sign up to request clarification or add additional context in comments.

5 Comments

if you were going to use os.listdir, if f.endswith(".mkv") would be sufficient
@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?
map(path.basename,iglob(pth+"*.mkv")) will be faster and more memory efficient
.endswith is also faster
@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.
7

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.

1 Comment

Thank you. I'm use your code like this => def list_images(input_dir): return list(map(os.path.abspath, iglob(input_dir + "\\" + "*.jpg")))
5

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

Comments

3

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)

Comments

1

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.

Comments

0

From os.walk you can read file paths as a list

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.