1
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!

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Pep
  • 13
  • 4
  • you need to provide an absolute path to the directory, another way if your directory is in the same with your py script – midori Feb 08 '16 at 22:22
  • Sorry, I hope I'm understanding you right, but at this time I'm inputting an absolute path, i.e. C:\\Users\\pep\\Documents – Pep Feb 08 '16 at 22:24

4 Answers4

1

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)
Prashanth
  • 183
  • 6
  • 15
L.S.
  • 476
  • 4
  • 10
  • This does appear to work! I assumed python would know that filename was just the last bit of the path. Thanks for the help. And thanks to everyone for the tips on incorporating raw strings. – Pep Feb 08 '16 at 23:22
0

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:
Community
  • 1
  • 1
Michal Frystacky
  • 1,418
  • 21
  • 38
0

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)
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • I like this idea. Any idea why it's throwing a syntax error at else? I can't figure that out. else: ^ SyntaxError: invalid syntax – Pep Feb 09 '16 at 00:10
  • Don't know. Usually it's because you have forgotten to close a bracket or something. I can't see any syntax errors in the code I posted. – Håken Lid Feb 09 '16 at 00:19
  • Thanks. Can't figure it out either but this was just an exercise anyway so I'm not too concerned. Thanks for your help! They say SO can be prickly with noob questions. – Pep Feb 09 '16 at 03:18
0

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.

Prashanth
  • 183
  • 6
  • 15