Ken Ho
Ken Ho

Reputation: 480

Print ERROR and hidden name

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

Answers (1)

André Laszlo
André Laszlo

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

Related Questions