merge csv files to one file
()
| 12 | |
| 13 | # the merge func |
| 14 | def merge(): |
| 15 | """merge csv files to one file""" |
| 16 | |
| 17 | # indicates use of a global variable. |
| 18 | global resultfile |
| 19 | |
| 20 | # use list save the csv files |
| 21 | csvfiles = [ |
| 22 | f |
| 23 | for f in os.listdir(".") |
| 24 | if f != resultfile and (len(f.split(".")) >= 2) and f.split(".")[1] == "csv" |
| 25 | ] |
| 26 | |
| 27 | # open file to write |
| 28 | with open(resultfile, "w") as writefile: |
| 29 | for csvfile in csvfiles: |
| 30 | with open(csvfile) as readfile: |
| 31 | print("File {} readed.".format(csvfile)) |
| 32 | |
| 33 | # do the read and write |
| 34 | writefile.write(readfile.read() + "\n") |
| 35 | print("\nFile {} wrote.".format(resultfile)) |
| 36 | |
| 37 | |
| 38 | # the main program |