简体   繁体   中英

Python: TypeError: coercing to Unicode: need string or buffer, module found

I am new to Python, just learning.

I am taking one of the file as input and I want to print the text on the console using python.

# This will take the file as input

import fileinput
for line in fileinput.input():

    print "The file name you provided is " + fileinput.filename()

    #file of = open(fileinput, "r", 0)
    with open(fileinput,'r') as myfile:
        data=myfile.read()
        print "This is your actual data \n\n" + data

The error I'm experiencing is:

TypeError: coercing to Unicode: need string or buffer, module found

Could any one please help me out on this please.

This particular problem is in the line below:

with open(fileinput,'r') as myfile:

fileinput is a module. I presume you mean to have:

with open(fileinput.filename(),'r') as myfile:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM