Python と sys および urllib2 モジュールを使用して、インターネットからファイルをダウンロードしようとしています。プログラムの背後にある一般的な考え方は、ユーザーがダウンロードしたいファイルのバージョン (たとえば 1_4) を入力することです。次に、プログラムはユーザー入力と「/whateverfile.jar」を URL に追加し、ファイルをダウンロードします。プログラムが「/whateverfile.jar」を新しい行に挿入するのと同じ行に挿入するのではなく、「/whateverfile.jar」を挿入すると、私の問題が発生します。これにより、プログラムが .jar を正しくダウンロードできなくなります。
誰でもこれで私を助けることができますか?コードと出力は以下のとおりです。
コード:
import sys
import urllib2
print('Type version of file you wish to download.')
print('To download 1.4 for instance type "1_4" using underscores in place of the periods.')
W = ('http://assets.file.net/')
X = sys.stdin.readline()
Y = ('/file.jar')
Z = X+Y
V = W+X
U = V+Y
T = U.lstrip()
print(T)
def JarDownload():
url = "T"
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()
出力:
Type version of file you wish to download.
To download 1.4 for instance type "1_4" using underscores in place of the periods.
1_4
http://assets.file.net/1_4
/file.jar
現在、画面に印刷されたときに URL が 1 行で表示されるまで、JarDownload() 関数をまったく呼び出していません。