Python file-io code listing current folder path instead of the specified

Posted by Tom Brito on Stack Overflow See other posts from Stack Overflow or by Tom Brito
Published on 2010-12-24T22:13:27Z Indexed on 2010/12/24 23:54 UTC
Read the original article Hit count: 338

Filed under:
|
|

I have the code:

import os
import sys

fileList = os.listdir(sys.argv[1])
for file in fileList:
    if os.path.isfile(file):
        print "File >> " + os.path.abspath(file)
    else:
        print "Dir >> " + os.path.abspath(file)

Located in my music folder ("/home/tom/Music")

When I call it with:

python test.py "/tmp"

I expected it to list my "/tmp" files and folders with the full path. But it printed lines like:

Dir >> /home/tom/Music/seahorse-gw2jNn
Dir >> /home/tom/Music/FlashXX9kV847
Dir >> /home/tom/Music/pulse-DcIEoxW5h2gz

This is, the correct file names, but the wrong path (and this files are not either in my Music folder).. What's wrong with this code?

© Stack Overflow or respective owner

Related posts about python

Related posts about file