Hi, I have the below script which downloads few files from server and writes the file size, start time and download time to a file. Now i want a header for that file. The script does so, but when i run the script again, again the header is appended to the start of the file.
# Downloading file and calculating download time
def download(url, dst_file):
start = time.time()
client = urlopen(url)
content = client.read()
size = client.headers["Content-Length"]
outfile = open(dst_file, "wb")
outfile.write(content)
outfile.close()
total = time.time() - start
total_time = '%s\t\t%s\t\t%s\n' % (size, datetime.datetime.now(), total)
download_time = str(total_time)
f = open("C:/downloadtime.txt", 'a')
f.write(download_time)
f.close()
if __name__ == "__main__":
file_name_1 = "autotest-1"
file_name_2 = "checksum_files"
file_name_3 = "db_sql_scripts"
file_name_4 = "pokermanager_linux_install.rar"
file_name_5 = "windows_release"
file_name_6 = "windows_server"
main_url = "http://10.47.42.28:8080/cruisecontrol/artifacts/Poker-TTM_autotest_nightly_build/20101224200424/"
file1_re = re.compile(r"%s" % file_name_1)
file2_re = re.compile(r"%s" % file_name_2)
file3_re = re.compile(r"%s" % file_name_3)
file4_re = re.compile(r"%s" % file_name_4)
file5_re = re.compile(r"%s" % file_name_5)
file6_re = re.compile(r"%s" % file_name_6)
for link in get_links(main_url):
if file1_re.search(link["href"]):
DOWNLOAD_URL1 = "http://10.47.42.28:8080" + link["href"]
download(DOWNLOAD_URL1, DOWNLOAD_DST1)
if file2_re.search(link["href"]):
DOWNLOAD_URL2 = "http://10.47.42.28:8080" + link["href"]
download(DOWNLOAD_URL2, DOWNLOAD_DST2)
if file3_re.search(link["href"]):
DOWNLOAD_URL3 = "http://10.47.42.28:8080" + link["href"]
download(DOWNLOAD_URL3, DOWNLOAD_DST3)
if file4_re.search(link["href"]):
DOWNLOAD_URL4 = "http://10.47.42.28:8080" + link["href"]
download(DOWNLOAD_URL4, DOWNLOAD_DST4)
if file5_re.search(link["href"]):
DOWNLOAD_URL5 = "http://10.47.42.28:8080" + link["href"]
download(DOWNLOAD_URL5, DOWNLOAD_DST5)
if file6_re.search(link["href"]):
DOWNLOAD_URL6 = "http://10.47.42.28:8080" + link["href"]
download(DOWNLOAD_URL6, DOWNLOAD_DST6)
headers = 'SIZE START_TIME DURATION'.split()
[B] for line in fileinput.input(['downloadtime.txt'], inplace=True):
if fileinput.isfirstline():
print '\t\t\t'.join(headers)
print line[/B]
How do i make it print the header just once?