Reputation: 81
I am trying to use shutil.move but getting error as below:
Traceback (most recent call last):
File "packageTest.py", line 202, in <module>
writeResult()
File "packageTest.py", line 92, in writeResult
shutil.move(tempfile,'report.csv')
File "/usr/local/lib/python2.7/shutil.py", line 294, in move
os.rename(src, real_dst)
TypeError: coercing to Unicode: need string or buffer, instance found
I am new to python and not sure what is wrong in this. Can anybody help me in this? I checked even whether i am trying to open the file twice but it is not so.
def writeResult():
tempfile=NamedTemporaryFile(delete=False)
result='FAIL'
with open('report.csv','rb') as infile,tempfile:
csvreader=csv.DictReader(infile)
fieldnames=csvreader.fieldnames
csvwriter=csv.DictWriter(tempfile,fieldnames)
csvwriter.writeheader()
for node,row in enumerate(csvreader,1):
if(row['EXIST_IN_BOM']=='Yes'):
if(row['EXIST_IN_TAR']=='No'):
csvwriter.writerow(dict(row,RESULT='FAIL'))
csvwriter.writerow(dict(row,COMMENT='File is missing in package'))
else:
result='PASS'
else:
if(row['EXIST_IN_TAR']=='Yes'):
csvwriter.writerow(dict(row,RESULT='FAIL'))
csvwriter.writerow(dict(row,COMMENT='File is missing in BOM'))
if(row['SIZE_IN_TAR']==row['SIZE_IN_ALPHA']):
result='PASS'
else:
csvwriter.writerow(dict(row,RESULT='FAIL'))
csvwriter.writerow(dict(row,COMMENT='File size not same'))
if(result=='PASS'):
csvwriter.writerow(dict(row,RESULT='PASS'))
shutil.move(tempfile,'report.csv')
return
Edit: The below code works though:
tempfile=NamedTemporaryFile(delete=False)
with open('dict.csv','rb') as infile,tempfile:
csvreader=csv.DictReader(infile)
fieldnames=csvreader.fieldnames
csvwriter=csv.DictWriter(tempfile,fieldnames)
csvwriter.writeheader()
for node,row in enumerate(csvreader,1):
if(row['EmpId']=='119093'):
csvwriter.writerow(dict(row,Rank='1'))
if(row['EmpId']=='119094'):
csvwriter.writerow(dict(row,Rank='2'))
shutil.move(tempfile.name,'dict.csv')
Upvotes: 0
Views: 744
Reputation: 798686
shutil.move()
takes two filenames, not a file-like and a filename. If you want to copy the contents of a file-like to a filename then open the filename in write mode and use shutil.copyfileobj()
instead.
Upvotes: 1