Guest User

read csv with filename column

a guest
Oct 23rd, 2019
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import glob
  3. import csv
  4.  
  5. # import csv files from folder and change directory
  6. path = r'file_path'
  7. os.chdir(path)
  8.  
  9. # create list of all files in directory
  10. allFiles = glob.glob(path + "/*.csv")
  11.  
  12. # read in each file, add filename column
  13. with open('output.csv', 'w') as csvoutput:
  14.    for i, fname in enumerate(allFiles):
  15.        with open(fname,'r') as csvinput:
  16.            if i != 0:
  17.                csvinput.readline()
  18.            writer = csv.writer(csvoutput, lineterminator='\n')
  19.            reader = csv.reader(csvinput)
  20.            print(os.path.basename(csvinput.name))
  21.            
  22.            all = []
  23.            row = next(reader)
  24.            row.append('FileName')
  25.            all.append(row)
  26.            
  27.            for row in reader:
  28.                row.append(os.path.basename(csvinput.name))
  29.                all.append(row)
  30.            writer.writerows(all)
  31.  
  32. File 1:
  33. Col1    Col2    Col3
  34. A       B       C
  35. A       B       C
  36. A       B       C
  37. A       B       C
  38.  
  39. File 2:
  40. Col14   Col5    Col6
  41. D       E       F
  42. D       E       F
  43. D       E       F
  44. D       E       F
  45.  
  46. Current output:
  47. Col1    Col2    Col3    FileName
  48. A       B       C       file_1.csv
  49. A       B       C       file_1.csv
  50. A       B       C       file_1.csv
  51. A       B       C       file_1.csv
  52. D       E       F       FileName  <---- script is not adding filename, but header
  53. D       E       F       file_2.csv
  54. D       E       F       file_2.csv
  55. D       E       F       file_2.csv
  56.  
  57. Expected output:
  58. Col1    Col2    Col3    FileName
  59. A       B       C       file_1.csv
  60. A       B       C       file_1.csv
  61. A       B       C       file_1.csv
  62. A       B       C       file_1.csv
  63. D       E       F       file_2.csv
  64. D       E       F       file_2.csv
  65. D       E       F       file_2.csv
  66. D       E       F       file_2.csv
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×