开发者

Python: delete the files after i finish reading from FTP

开发者 https://www.devze.com 2023-04-11 20:26 出处:网络
My python script opens a gzip file from FTP and reads it. Every time I run the script .gz files are downloaded to my Harddisk (Sites/htdocs folder as its a python cgi). I dont want to download the fil

My python script opens a gzip file from FTP and reads it. Every time I run the script .gz files are downloaded to my Harddisk (Sites/htdocs folder as its a python cgi). I dont want to download the files to my harddisk or else delete the files after execution of script.

Here is snapshot of my script:

u = 'http://hapmap.ncbi.nlm.nih.gov/downloads/frequencies/2010-05_phaseIII/'

filename1 = 'allele_freqs_chr' + chromosomes[i] + '_' + populations[0] + '_phase3.3_nr.b36_fwd.txt.gz'
url = u + filename1
try:
    site = urllib.urlretrieve(url,filename1)
except IOError:
    print >> sys.stderr,'Error opening URL.\n'
try: 
    f1 = gzip.open(filename1, 'rb')
except IOError:
    print >> sys.stderr, 'Error opening file1.\n'
    sys.exit(1) 

line=f1.readline()
# ...

I ap开发者_开发百科preciate your suggestions.


os.unlink(filename1) should work. Also, use finally: in your try: block to close the file descriptor like so:

import os
u = 'http://hapmap.ncbi.nlm.nih.gov/downloads/frequencies/2010-05_phaseIII/'
filename1 = 'allele_freqs_chr' + chromosomes[i] + '_' + populations[0] + '_phase3.3_nr.b36_fwd.txt.gz'
url = u + filename1
try:
    site = urllib.urlretrieve(url,filename1)
except IOError:
    print >> sys.stderr,'Error opening URL.\n'
try: 
    f1 = gzip.open(filename1, 'rb')
except IOError:
    print >> sys.stderr, 'Error opening file1.\n'
    sys.exit(1)
else:
    line = f1.readline()
    # ....
finally:
    try:
        f1.close()
    except:
        pass

os.unlink(filename1)


You can use urllib.urlopen instead of urllib.urlretrieve

fd = urllib.urlopen(url)
s_data = fd.read()                    # These 2 lines are unfortunately
s_stream = StringIO.StringIO(s_data)  # needed in Python < 3.2
f1 = gzip.GzipFile(fileobj=s_stream)

See also: http://www.enricozini.org/2011/cazzeggio/python-gzip/ (On why you have to use StringIO)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号