2012-03-09 5 views
4

私は最近、pythonで特定のディレクトリにファイルをダウンロードするプログラムを作成しようとしています。私はUbuntuのを使用していますが、これまでのところ、私はこのファイルを特定のディレクトリにダウンロードするにはどうすればよいですか?

import os 
import getpass 
import urllib2 

y = getpass.getuser() 

if not os.access('/home/' + y + '/newdir/', os.F_OK): 
    print("Making New Directory") 
    os.mkdir('/home/' + y + '/newdir/') 

url = ("http://example.com/Examplefile.ex") 
file_name = url.split('/')[-1] 
u = urllib2.urlopen(url) 
f = open(file_name, 'wb') 
meta = u.info() 
file_size = int(meta.getheaders("Content-Length")[0]) 
print "Downloading: %s Bytes: %s" % (file_name, file_size) 

file_size_dl = 0 
block_sz = 8192 
while True: 
    buffer = u.read(block_sz) 
    if not buffer: 
    break 

    file_size_dl += len(buffer) 
    f.write(buffer) 
    status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100./file_size) 
    status = status + chr(8)*(len(status)+1) 
    print status, 

f.close() 

これは、現在、私はそれがダウンロードディレクトリを変更することができる方法と同じディレクトリにファイルをダウンロードする必要がありますか?

import os 
import getpass 
import urllib2 

y = getpass.getuser() 

if not os.access('/home/' + y + '/newdir/', os.F_OK): 
    print("Making New Directory") 
    os.mkdir('/home/' + y + '/newdir/') 

os.chdir('/home/'+y+'/newdir/') 

url = ("http://example.com/Examplefile.ex") 
file_name = url.split('/')[-1] 
u = urllib2.urlopen(url) 
f = open(file_name, 'wb') 
meta = u.info() 
file_size = int(meta.getheaders("Content-Length")[0]) 
print "Downloading: %s Bytes: %s" % (file_name, file_size) 

file_size_dl = 0 
block_sz = 8192 
while True: 
    buffer = u.read(block_sz) 
    if not buffer: 
    break 

    file_size_dl += len(buffer) 
    f.write(buffer) 
    status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100./file_size) 
    status = status + chr(8)*(len(status)+1) 
    print status, 

f.close() 
+0

'/ home /%s/newdir /%s '%(y、url)と言うように' file_name'を変更することができます。分割( '/')[ - 1]) '? –

+0

mathematical.coffeeこれは私に変数uのエラーを与えます – MacroSacramento

+0

'u'は' f'とは関係がないので、どうなっているのか分かりません。私の提案は@ IgnaciaVazquez-Abramsと同じです。あなたの 'open(file_name、 'wb')には' file_name'に保存したいディレクトリが含まれていればいいだけです( 'file_name'は'/home/foo/Example.ex'と 'Example.ex'とでは異なります)。私が与えたコードスニペットは 'file_name'を変更する一つの方法でした。 –

答えて

2

申し訳ありませんがみんな、私は愚かされていたが、私は右の最初のif文のEX後

os.chdir('/home/' + y + '/newdir/') 

を追加した質問に答えるために:file_nameにディレクトリを追加する

import os 
import getpass 
import urllib2 

y = getpass.getuser() 

if not os.access('/home/' + y + '/newdir/', os.F_OK): 
    print("Making New Directory") 
    os.mkdir('/home/' + y + '/newdir/') 

os.chdir('/home/'+y+'/newdir/') 

url = ("http://example.com/Examplefile.ex") 
file_name = url.split('/')[-1] 
u = urllib2.urlopen(url) 
f = open(file_name, 'wb') 
meta = u.info() 
file_size = int(meta.getheaders("Content-Length")[0]) 
print "Downloading: %s Bytes: %s" % (file_name, file_size) 

file_size_dl = 0 
block_sz = 8192 
while True: 
    buffer = u.read(block_sz) 
    if not buffer: 
    break 

    file_size_dl += len(buffer) 
    f.write(buffer) 
    status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100./file_size) 
    status = status + chr(8)*(len(status)+1) 
    print status, 

f.close() 
0

ファイル名にopen()にディレクトリを渡す:

は、それが新しいコードに固定しました。

+0

と私はどのようにディレクトリを渡すだろうか? – MacroSacramento

+0

同じ方法でファイル名を渡します。特にファイル名の一部です。 –

関連する問題