0

I'm trying to open each file from a directory and print the contents, so I have a code as such:

import os, sys

def printFiles(dir):
    os.chdir(dir)
    for f in os.listdir(dir):
        myFile = open(f,'r')
        lines = myFile.read()
        print lines
        myFile.close()

printFiles(sys.argv[1])

The program runs, but the problem here is that it is only printing one of the contents of the file, probably the last file that it has read. Does this have something to do with the open() function?

Edit: added last line that takes in sys.argv. That's the whole code, and it still only prints the last file.

11
  • Are you sure? What happens if you just print f within your loop - how many filenames is it showing it's looping over? Commented Aug 12, 2016 at 19:05
  • @JonClements when I print f it does print all the file names in the directory. Commented Aug 12, 2016 at 19:06
  • @maregor Are some files empty for instance? Commented Aug 12, 2016 at 19:06
  • @JonClements no, they all have contents. It still prints the last file of any directory only. Commented Aug 12, 2016 at 19:13
  • Why do you call os.chdir()? Commented Aug 12, 2016 at 19:19

2 Answers 2

1

There is problem with directory and file paths.

Option 1 - chdir:

def printFiles(dir):
    os.chdir(dir)
    for f in os.listdir('.'):
        myFile = open(f,'r')
        # ...

Option 2 - computing full path:

def printFiles(dir):
    # no chdir here
    for f in os.listdir(dir):
        myFile = open(os.path.join(dir, f), 'r')
        # ...

But you are combining both options - that's wrong.

This is why I prefer pathlib.Path - it's much simpler:

from pathlib import Path

def printFiles(dir):
    dir = Path(dir)
    for f in dir.iterdir():
        myFile = f.open()
        # ...
Sign up to request clarification or add additional context in comments.

5 Comments

I'm having difficulty getting the first and second options to work. They still result in printing the last file only... I prefer if I didn't have to import but I would use it if no other options are available.
@maregor: it works for me... gist.github.com/messa/…
So it does... I tried editing my code to have the same exact lines as yours and it doesn't work, but when I copy paste your code into mines it does. It completely bewilders me.
@maregor: Are you mixing tabs and spaces? :) If that print line ends up on the same level as the for (instead of being in the for block) it would have the exact same results as you are reporting.
Thanks. I think that was the cause.
0

The code itself certainly should print the contents of every file. However, if you supply a local path and not a global path it will not work.

For example, imagine you have the following folder structure:

./a
./a/x.txt
./a/y.txt
./a/a
./a/a/x.txt 

If you now run

printFiles('a')

you will only get the contents of x.txt, because os.listdir will be executed from within a, and will list the contents of the internal a/a folder, which only has x.txt.

1 Comment

I did supply the full path, and I had only 3 files in it, but it would just print out the last file in any directory that I put into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.