Reputation: 131
my files names look like this:
data00001
data00002
data00003
....
data00010
....
i have them saved and want to download them from the website they're in using this code:
import urllib.request as urllib2
response = urllib2.urlopen("https://somewebsite/data00001.mat")
file = open(r"C:\Users\me\Desktop\data\data00001.mat", 'wb')
file.write(response.read())
file.close()
i shall give the starting and end file name for example:
from data00001 to data01000 so in this example i want the files from data0001 to data01000 downloaded. how can i increment the numeric value near data to make that work?
Upvotes: 0
Views: 99
Reputation: 1839
try this:
for i in range(1, 1001):
filename = "data" + str(i).zfill(4) + ".mat" # gives a padding of four digits
You can use filename for all your operations
Upvotes: 5
Reputation: 6914
You can use integer range and formatted print with zero padding:
import urllib.request as urllib2
for i in range(1000):
filename = 'data{:05d}.mat'.format(i)
response = urllib2.urlopen("https://somewebsite/" + filename)
file = open(r"C:\Users\me\Desktop\data\" + filename, 'wb')
file.write(response.read())
file.close()
Upvotes: 0
Reputation: 7846
It is fairly easy to retrieve all your files within a loop:
import urllib.request as urllib2
counter = 1
limit = 1000
filename = "data"
for i in range(limit+1):
newFilename = "".join([filename,str(i).zfill(5)])
response = urllib2.urlopen("https://somewebsite/" + newFilename + ".mat")
file = open(r"C:\\Users\\me\\Desktop\\data\\" + newFilename + ".mat", 'wb')
file.write(response.read())
file.close()
Upvotes: 0