Python:从 FTP 读取完后删除文件

发布于 2024-12-09 05:35:07 字数 679 浏览 0 评论 0原文

我的 python 脚本从 FTP 打开一个 gzip 文件并读取它。每次我运行脚本时,.gz 文件都会下载到我的硬盘上(Sites/htdocs 文件夹作为 python cgi)。我不想将文件下载到我的硬盘上,或者在执行脚本后删除文件。

这是我的脚本的快照:

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()
# ...

我感谢您的建议。

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 appreciate your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-12-16 05:35:07

os.unlink(filename1) 应该可以工作。另外,在 try: 块中使用 finally: 来关闭文件描述符,如下所示:

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)

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)
风透绣罗衣 2024-12-16 05:35:07

您可以使用 urllib.urlopen 代替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)

另请参阅:http://www.enricozini.org/2011/cazzeggio/python-gzip/ (关于为什么必须使用 StringIO)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文