Reputation: 245
I'm new to programing especially Python. But i need this program to work for research purposes. The error looks like this:
('read db data from ==>', u'C://Users//DBF.txt', 'and input file data from ==>', u'C://Users//IF.txt')
<open file 'C://Users//OpPanda_S_R', mode 'r' at 0x0000000006309030>
Traceback (most recent call last):
File "C:/Users/Test.py", line 48, in <module>
if op.exists(f) & op.getsize(f):
File "C:\Users\genericpath.py", line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, file found
Process finished with exit code 1
This is the relevant part of the code:
f = open(str(outputFileGroupName)+"_"+str(group))
print f
if op.exists(f) & op.getsize(f)>0:
if globals()["same_market_"+str(group)+"_file"].shape[0] > 0: globals()["same_market_"+str(group)+"_file"].to_csv(outputFileGroupName, index=False,mode='a',header=False)
pass
if ~contolGroupValidityCheck:
new_entry_occured.append(diff_market_plan_file).to_csv(globals()[str(outputFileGroupName)+"_tmp"], index=False)
I did check these posts, TypeError: coercing to Unicode: need string or buffer, file found Python TypeError: coercing to Unicode: need string or buffer, file found TypeError: coercing to Unicode: need string or buffer, dict found Python writing to CSV... TypeError: coercing to Unicode: need string or buffer, file found, but i couldn't find suitable solution.
I'd be very glad if somebody could help a noob out here. I'm sure you "pros" already get the problem when you just look at it. So can somebody tell me why i'm getting this particular error at line 48?I can see that in the said directory the file has been created, i'm just checking whether the file is existing and if its length is greater that 0 then accordingly do respective statements.
Upvotes: 1
Views: 7827
Reputation: 140196
f = open(str(outputFileGroupName)+"_"+str(group))
At this point, f
is a handle on the file. os.path.exists
and os.path.getsize
expect a filename.
So check for existence/size first using the name, and open it afterwards.
name = str(outputFileGroupName)+"_"+str(group)
if op.exists(name) and op.getsize(name)>0:
f = open(name)
and don't use logical &
for multiple conditions as short-circuit wouldn't work and if file doesn't exist, the getsize
is still called and you get an IOError
. Use and
Aside: same thing for if ~contolGroupValidityCheck
in your code. Use not
. Reserve ~
, &
... for bitwise operation, not logical operations.
Upvotes: 1