miitak
miitak

Reputation: 19

Python os.remove failure (unicode object?)

My python script reads JSON information from a website, stores it in a file for processing, and should clean it in the end.

This was working without issues in other scripts, but for some reason, os.remove fails to delete the file in the end:

import urllib2, json
import os, sys, argparse


ref_list_tmpfile = '/tmp/reference.%s.txt' % os.getpid()
ref_list_response=urllib2.urlopen('http://localhost:11111/api/reference').read()

with open(ref_list_tmpfile,'w') as outfile:
outfile.write(ref_list_response)

ref_list_data=open(ref_list_tmpfile)
reference_list = json.load(ref_list_data)
ref_list_data.close()
.
.
.
.
os.remove(ref_list_tmpfile)

The main logic works well, but the error i'm getting refers to the last command (os.remove) and the file is not deleted:

Traceback (most recent call last):
  File "./vm_creator.py", line 58, in <module>
    os.remove(ref_list_tmpfile)
AttributeError: 'unicode' object has no attribute 'remove'

Any ideas?

Upvotes: 0

Views: 353

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

You've redefined os to be a string, somewhere in the code you've snipped.

Upvotes: 1

Related Questions