How to iterate over files using Python?

I have a folder with ten files that I want to skip. When I print the file name, my code works fine:

import os indir = '/home/des/test' for root, dirs, filenames in os.walk(indir): for f in filenames: print(f) 

What prints:

 1 2 3 4 5 6 7 8 9 10 

But if I try to open the file in a loop, I get an I / O error:

 import os indir = '/home/des/test' for root, dirs, filenames in os.walk(indir): for f in filenames: log = open(f, 'r') Traceback (most recent call last): File "/home/des/my_python_progs/loop_over_dir.py", line 6, in <module> log = open(f, 'r') IOError: [Errno 2] No such file or directory: '1' >>> 

Do I need to transfer the full file path even inside the loop?

+55
python
Aug 03 '12 at 18:30
source share
5 answers

Yes, you need the full path.

 log = open(os.path.join(root, f), 'r') 

Quick fix. As noted in the comment, os.walk subdivided, so you need to use the current root directory, not indir as the base for connecting the path.

+23
Aug 03 '12 at 18:32
source share
β€” -

If you are just looking for files in the same directory (i.e. you are not trying to traverse a directory tree that it does not look like), why not just use os.listdir () :

 import os for fn in os.listdir('.'): if os.path.isfile(fn): print (fn) 

instead of os.walk () . You can specify the directory path as a parameter for os.listdir () . os.path.isfile () will determine if the given file name is for the file.

+84
Aug 03 '12 at 18:32
source share

You must specify the path you are working on:

 source = '/home/test/py_test/' for root, dirs, filenames in os.walk(source): for f in filenames: print f fullpath = os.path.join(source, f) log = open(fullpath, 'r') 
+6
Jan 31 '16 at 13:15
source share

The examples for os.walk in the documentation show how to do this:

 for root, dirs, filenames in os.walk(indir): for f in filenames: log = open(os.path.join(root, f),'r') 

How did you expect the "open" function to know that the string "1" should mean "/ home / des / test / 1" (if "/ home / des / test" is not your current working directory)?

+4
Aug 03 '12 at 18:34
source share

Here is a snippet that will process the file tree for you:

 indir = '/home/des/test' for root, dirs, filenames in os.walk(indir): for f in filenames: print(f) log = open(indir + f, 'r') 
+4
Aug 06 '15 at 19:49
source share



All Articles