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?

I have no idea which output file you are talking about or which statement writes the header a second time. If you are talking about this segment of the code,

outfile = open(dst_file, "wb")
    outfile.write(content)
    outfile.close()

"wb" creates/overwrites to a new file each time the function is called. You can also replace a lot of this code with a list:

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)
##-----------------------------------------------------------------------------
##   replace all of this with the following (NOT TESTED)
##-----------------------------------------------------------------------------
    file_list = ["autotest-1", "checksum_files", "db_sql_scripts", \
        "pokermanager_linux_install.rar", "windows_release", "windows_server"]
    DOWNLOAD_DST = ["name1", "name2", "name3", "name4", "name5", "name6"]

    main_url = "http://10.47.42.28:8080/cruisecontrol/artifacts/"
    main_url += "Poker-TTM_autotest_nightly_build/20101224200424/"
    for link in get_links(main_url):
        for ctr, file_name in enumerate(file_list):
            file_re = re.compile(r"%s" % file_name)
            if file_re.search(link["href"]):
                DOWNLOAD_URL = "http://10.47.42.28:8080" + link["href"]
                download(DOWNLOAD_URL, DOWNLOAD_DST[ctr])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.