Pep February 2016

Beginner Python 3--os.path and WinError2

import os

searchFolder = input('Which folder would you like to search?')

def search(folder):
    for foldername, subfolders, filenames in os.walk(folder):
        for filename in filenames:  
            if os.path.getsize(filename) > 1000:
                print(str(os.path.abspath(filename)) + 'is ' + str(os.path.getsize(filename)))
            else:
                continue

search(searchFolder)

This program is meant to ask the user for a string, iterate over the files in that directory, and print the abs path and file size of every item over a certain size. I'm getting a FileNotFoundError: [WinError 2] when I run this code, on any directory. I'm inputting the directory with escaped backslashes. I think this is such a rudimentary error on my part that this is all the info anyone would need but let me know if there's anything else that would be helpful. Thanks!

Answers


Michal Frystacky February 2016

I tested the code and it works fine, I used for my test. ./

Python accepts both path types:

path = "C:/" # unix

and

path = "C:\\" # windows

for input try ./ , which will search the directory the program is in. So, you have two options, relative pathing or absolute pathing.

More on pathing.

Although as was mentioned, for anything outside of the programs directory you need to correct the line

if os.path.getsize(filename) > 1000:

to

if os.path.getsize(foldername+"/"+filename) > 1000:


L.S. February 2016

In the filename for loop you have only passed the filename but not the complete path. If you write:

if os.path.getsize(foldername+"/"+filename) > 1000:

This works for linux. For Windows you need to use \ or \\instead of /. So now you understand why it isn't working. You should use the full filepath or relative path while adding a path.

Working code in linux:

import os

searchFolder = input('Which folder would you like to search? ')

def search(folder):
    for foldername, subfolders, filenames in os.walk(folder):
        for filename in filenames:
            if os.path.getsize(foldername+"/"+filename) > 1000:
            print(str(os.path.abspath(filename)) + ' is ' + str(os.path.getsize(foldername+"/"+filename)))
        else:
            continue

search(searchFolder)


Håken Lid February 2016

Input() will return the string that the user writes. You don't have to escape backslashes. So just input it as C:\path\to\my\folder\. It's when you write windows paths in your python source code that you must escape your backslashes or use r"raw string".

You can use os.path.isdir() to check that python actually accepts the path, and print an error if the path could not be found.

searchFolder = input('Which folder would you like to search?')
if os.path.isdir(searchFolder):
    search(searchFolder)
else:
    print("the folder %s was not found" % searchFolder)


Prashanth February 2016

Whenever you want to insert any path, just add an r before the path. This is Python's raw string notation. i.e; backslashes are not handled in any special way in a string literal prefixed with r

So, if you want to add a path to a file called foo in C:\Users\pep\Documents

Just give your path as

my_path = r'C:\Users\pep\Documents\foo'

You don't need to bother escaping any backslashes now.

Post Status

Asked in February 2016
Viewed 3,315 times
Voted 14
Answered 4 times

Search




Leave an answer