Michael Mormon
Michael Mormon

Reputation: 559

Printing File Names

I am very new to python and just installed Eric6 I am wanting to search a folder (and all sub dirs) to print the filename of any file that has the extension of .pdf I have this as my syntax, but it errors saying

The debugged program raised the exception unhandled FileNotFoundError
"[WinError 3] The system can not find the path specified 'C:'"
File: C:\Users\pcuser\EricDocs\Test.py, Line: 6

And this is the syntax I want to execute:

import os

results = []
testdir = "C:\Test"
for folder in testdir:
  for f in os.listdir(folder):
    if f.endswith('.pdf'):
        results.append(f)

print (results)

Upvotes: 13

Views: 60242

Answers (8)

Imran S M
Imran S M

Reputation: 525

I had to mention the names of training images for my Yolo model,
Here's what i did to print names of all images which i kept for training YoloV3 Model

import os

for root, dirs, files in os.walk("."):
    for filename in files:
        print(filename)

It prints out all the file names from the current directory

Upvotes: 1

Inbar Rose
Inbar Rose

Reputation: 43497

Use the glob module.

The glob module finds all the pathnames matching a specified pattern

import glob, os
parent_dir = 'path/to/dir'
for pdf_file in glob.glob(os.path.join(parent_dir, '*.pdf')):
    print (pdf_file)

This will work on Windows and *nix platforms.


Just make sure that your path is fully escaped on windows, could be useful to use a raw string.

In your case, that would be:

import glob, os
parent_dir = r"C:\Test"
for pdf_file in glob.glob(os.path.join(parent_dir, '*.pdf')):
    print (pdf_file)

For only a list of filenames (not full paths, as per your comment) you can do this one-liner:

results = [os.path.basename(f) for f in glob.glob(os.path.join(parent_dir, '*.pdf')]

Upvotes: 19

Iron Fist
Iron Fist

Reputation: 10961

You are basically iterating through the string testdir with the first for loop then passing each character to os.listdir(folder) does not make any sense then, just remove that first for loop and use fnmatch method from fnmatch module:

import os
from fnmatch import fnmatch

ext = '*.pdf'
results = []
testdir = "C:\Test"
for f in os.listdir(testdir):
    if fnmatch(f, ext):
        results.append(f)

print (results)

Upvotes: 2

BChow
BChow

Reputation: 471

You will need to escape the backslash on windows and you can use os.walk to get all the pdf files.

   for root,dirs,files in os.walk(testdir):
       for f in files:
           if f.endswith('.pdf'):
            results.append(f)
   print (results)

Upvotes: 2

Tony Babarino
Tony Babarino

Reputation: 3405

Try testdir = r"C:\Test" instead of testdir = "C:\Test". In python You have to escape special characters like for example \. You can escape them also with symbol '\' so it would be "C:\\Test". By using r"C:\Test", You are telling python to use raw string.

Also for folder in testdir: line doesn't make sense because testdir is a string so You are basically trying to iterate over a string.

Upvotes: 1

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

There are a few problems in your code, take a look at how I've modified it below:

import os

results = []
testdir = "C:\\Test"
for f in os.listdir(testdir):
    if f.endswith('.pdf'):
        results.append(f)

print (results)

Note that I have escaped your path name, and removed your first if folder.... That wasn't getting the folders as you expected, but rather selecting a character of the path string one at a time.

You will need to modify the code to get it to look through all folders, this currently doesn't. Take a look at the glob module.

Upvotes: 2

Sean Francis N. Ballais
Sean Francis N. Ballais

Reputation: 2488

Try running your Python script from C:. From the Command Prompt, you might wanna do this:

> cd C:\    
> python C:\Users\pcuser\EricDocs\Test.py

As pointed out by Tony Babarino, use r"C:\Test" instead of "C:\Test" in your code.

Upvotes: 2

mattsap
mattsap

Reputation: 3806

Right now, you search each character string inside of testdir's variable.

so it's searching the folder for values "C", ":", "\", "T" etc. You'll want to also escape your escape character like "C:\...\...\"

You probably was to use os.listdir(testdir) instead.

Upvotes: 4

Related Questions