Reputation: 480
I have .txt file have lines like
.txt
A
B
C
I use the following code to create some new csv file.
with open(name, "rb") as f:
name = f.readlines()
for i in files:
open(path+'%s.csv' %i, "w")
However, when I use code
tb = [ f for f in os.listdir(a) if os.path.isfile(os.path.join(a,f))]
for i in tb:
print i
Result
A
.csv
B
.csv
C
.csv
it should be
A.csv
B.csv
C.csv
Upvotes: 2
Views: 54
Reputation: 15537
Readlines return each line with the trailing newline character(s) intact. Add a call to strip()
:
open(path+'%s.csv' % i.strip(), "w")
Upvotes: 1