-1

I am learning how to write python. As a test I want to print the names of all files bigger than 1GB here is my code so far:

path = 'C:\\Users\\brentond\\Documents\\Outlook Files'
for filename in path:
    if os.path.getsize(os.path.join(path, filename))>1000000000:
        print(filename)

I get this error

SyntaxError: multiple statements found while compiling a single statement
rivamarco
  • 719
  • 8
  • 23
Dan
  • 95
  • 1
  • 13
  • 4
    `filename` here is each character of the string `path` – Chris_Rands Dec 02 '19 at 14:17
  • 1
    Also, from [the error](https://stackoverflow.com/q/21226808/1639625) it seems like you pasted this into an interactive shell instead of creating and running a `.py` file. – tobias_k Dec 02 '19 at 14:20

3 Answers3

1

You need to replace path with os.listdir() which will return an iterable of files in your path.

for filename in os.listdir(path)
zglin
  • 2,891
  • 2
  • 15
  • 26
0

you should use os.listdir instead:

 import os
 for filename in os.listdir(path):

and then the rest of your code

jorop
  • 483
  • 4
  • 4
0

In ypur case, path is just a string, and looping through it will return each character. you have to use os.listdir() to get a list.

Also, you could test for file first, so you don't get size for evry directory as well.

here is the full code

for filename in os.listdir(path):
    if os.path.isfile(os.path.join(path, filename)) and os.path.getsize(os.path.join(path, filename))>1000000000:
        print(filename)

Petru Tanas
  • 1,087
  • 1
  • 12
  • 36