I need to take a list of files and remove a '.2' from a line in each file. I then need to rewrite the file without the '.2'. Using the code below I am >able to take a single file and do exactly what i need to but when trying to use glob.glob to rewrite a number of files I run into problems and >was wondering if anyone could help.
when i run this code i get the following error.
File "learn.py", line 15, in moduleTypeError: coercing to Unicode: need string or buffer, list found
dwhite12 0 Newbie Poster
Recommended Answers
Jump to PostTo post a code sample, you can use the Code button on the editing bar just above the editing pane. This should open up a new window where you can paste the code samples. Alternately, you can simply indent the line four spaces, with extra lines between the code and …
Jump to Postfor line in files:
It does not make sense to call filename line. Your indention is incorrect.
Jump to PostNow that your got it working, may I give you my version on single file read-write
with open(infile_name) as infile, open(infile_name+'_') as outfile: outfile.write(infile.read().replace('.2','')) os.remove(infile_name) os.rename(infile_name+'_', infile_name)
All 11 Replies
dwhite12 0 Newbie Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
dwhite12 0 Newbie Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
dwhite12 0 Newbie Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
dwhite12 0 Newbie Poster
dwhite12 0 Newbie Poster
dwhite12 0 Newbie Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Lucaci Andrew 140 Za s|n
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.