yusuf
yusuf

Reputation: 3781

OSError: [Errno 2] No such file or directory in Python when reading "gz" file

I have a folder which involves ".gz" files.

import matplotlib.pylab as plt, os

from os import listdir
from os.path import isfile, join

mypath = '/export/students/sait/yedek'

onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

size = []

for a in range(len(onlyfiles)):
        print onlyfiles[a]
        size.append(os.path.getsize(onlyfiles[a]))
        print size[a]

When I want to read the sizes, I get such an error:

"OSError: [Errno 2] No such file or directory: 'rgb-0.ppm.gz'"

Where is this smart problem?

Upvotes: 1

Views: 943

Answers (2)

Suraj
Suraj

Reputation: 168

import matplotlib.pylab as plt, os
from os import listdir
from os.path import isfile, join
mypath = os.path.join(os.getcwd(),'/export/students/sait/yedek')
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
size = []
for file in onlyfiles:
        print file
        file_size = os.path.getsize(file)
        print file_size
        size.append(file_size)

try this this is working for me

Upvotes: 1

Dmitry Ermolov
Dmitry Ermolov

Reputation: 2237

Your onlyfiles contains basename of the files, not the full paths to them. (and your working directory seems to be not mypath).

You either need to fix onlyfiles creation: onlyfiles = [join(mypath, f) for f in listdir(mypath) if isfile(join(mypath, f))]

or you can also os.chdir(mypath) before your for-loop.

Upvotes: 2

Related Questions