python 3.x - How to resolve syntax error on print file=file in linux -


i new python. receiving syntax error on file=file_out. below error, code after that. have been copying , pasting code internet. ^ under = between file=file_out on print line.

error:

file "./xxx.py", line 18
print ("%s"%(line), file=file_out)
^

code:

import fnmatch import os  rootpath = '/xxx/xxx//' pattern = 'xxx.txt'  file_in = open(os.path.join(root, filename),"r") file_out = open("output.txt", "w")  root, dirs, files in os.walk(rootpath): filename in fnmatch.filter(files, pattern):     print( os.path.join(root, filename))     line in file_in:             print ("%s"%(line), file=file_out)     file_in.close()     file_out.close() 

you're running python 2, file keyword hadn't been introduced.

change print ("%s"%(line), file=file_out) line file_out.write(line+"\n"), equivalent, simpler, , works in python versions far.

but since file iterator issues lines end-of-line included, don't want 2 newlines write file_out.write(line)


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -