0

im having problems trying to run an iteration over many files in a folder, the files exist, if I print file from files I can see their names... Im quite new to programming, could you please give me a hand? kind regards!

import os
for path, dirs, files in os.walk('FDF\FDF'):
    for file in files:
        print file
        fdf = open(file, "r")
IOError: [Errno 2] No such file or directory: 'FDF_20110612_140613_...........txt'
mnagel
  • 6,729
  • 4
  • 31
  • 66
Patowski
  • 5
  • 1
  • 4
  • http://stackoverflow.com/a/9765314/1350424 or http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects – eclipsis Aug 05 '13 at 21:15
  • you should add tags with the technologies you use (python in this case) so your question will appear at the relevant places. i added it for you. – mnagel Aug 05 '13 at 21:28
  • i am not experienced with python, but that backslash seems odd to me `'FDF\FDF'`. paths usually use `/`. – mnagel Aug 05 '13 at 21:31
  • And of [Having Problems with Arguments](http://stackoverflow.com/q/11825923) and [Trying to identify the newest and second newest file in a directory](http://stackoverflow.com/q/12715309) – Martijn Pieters Aug 05 '13 at 21:42
  • thanks for oyur inouts, regards – Patowski Aug 05 '13 at 22:08

2 Answers2

0

You need to prefix each file name with path before you open the file.

See the documentation for os.walk.

import os
for path, dirs, files in os.walk('FDF\FDF'):
    for file in files:
        print file
        filepath = os.path.join(path, file)
        print filepath
        fdf = open(filepath, "r")
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Try this:

import os

for path, dirs, files in os.walk('FDF\FDF'):
    for file in files:
        print file
        with open(os.path.join(path, file)) as fdf:
            # code goes here.
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96