'Python code to batch convert multiple non-csv files to csv files in a given directory
I am trying to batch convert anything other than a csv in a given directory to csv files(In this case, dat, txt files with the same filename)
import pandas as pd
import glob, os
parent_dir = 'c/path'
for dat_file in glob.glob(os.path.join(parent_dir, '*.dat')):
#print (dat_file)
fln = print(os.path.basename(dat_file))
#print(fln)
df = pd.read_csv(fln + ".dat", sep='|', header=None)
hdr = ['a', 'b', 'c', 'd']
df.to_csv(fln + ".csv", header=hdr, index=False)
for txt_file in glob.glob(os.path.join(parent_dir, '*.txt')):
#print (txt_file)
fln = print(os.path.basename(txt_file))
#print(fln)
df = pd.read_csv(fln + ".txt", sep='', header=None)
hdr = ['a', 'b', 'c', 'd']
df.to_csv(fln + ".csv", header=hdr, index=False)
But when I am trying to execute, it says, although the syntax seems correct.
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
