Domanda

I want to remove a file. That file was created earlier by the same program that I want to delete it.

This is what I have:

USER_FILE_NAME = 'user_info.json'
file_to_delete = os.path.join(ADDON_DATA['profile_dir'], USER_FILE_NAME)
xbmc.log('File to delete:')
print(file_to_delete)
os.remove(file_to_delete)

This is What I get (usernames and the likes were redacted due to paranoid tendencies):

NOTICE: File to delete:
NOTICE: C:\Users\USERNAME\AppData\Roaming\XBMC\userdata\addon_data\script.NAME\user_info.json
NOTICE: [Errno 2] No such file or directory: u'C:\\Users\\USERNAME\\AppData\\Roaming\\XBMC\\userdata\\addon_data\\script.NAME\\user_info.json'

I think it is because of those damn slashes. I tried all sorts of things, but nothing seems to work. No matter what I did to the string in file_to_delete, including but really not limited to replacing the backslashes with forward slashes, the file it tried to find and delete was always the same as in the error line.

Can anyone help me?

UPDATE:

So I tried something. I added os.remove(repr(file_to_delete)) after the original os.remove(file_to_delete), so I now have this:

if os.path.exists(file_to_delete):
    xbmc.log('User file exists, Prepairing to delete')
    xbmc.log('stats: %s' % os.access(file_to_delete, os.F_OK))
    xbmc.log('stats: %s' % os.access(file_to_delete, os.W_OK))
    xbmc.log('stats: %s' % os.access(file_to_delete, os.X_OK))
    os.remove(file_to_delete)
    os.remove(repr(file_to_delete))

If I comment out os.remove(repr(file_to_delete)), I get the same error as before. If I don't comment that one but do the one above it, I get this error:

User file exists, Prepairing to delete
stats: True
Previous line repeats 2 times.
ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
.
.
.
os.remove(repr(file_to_delete))
    WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: "u'C:\\\\Users\\\\USERNAME\\\\AppData\\\\Roaming\\\\XBMC\\\\userdata\\\\addon_data\\\\script.NAME\\\\user_info.json'"

If I don't comment either one and run in like it is, I get the same error as above but the file IS REMOVED. Any ideas as to why this is happening?

UPDATE 2:

The behavior above also happens in this instance:

os.remove(file_to_delete)
os.remove(file_to_delete)

File is removed, but get the error:

WindowsError: [Error 2] The system cannot find the file specified: u'C:\\Users\\USERNAME\\AppData\\Roaming\\XBMC\\userdata\\addon_data\\script.NAME\\user_info.json'

When I use os.remove() with a different file in the same folder, both built with os.path.join(), once is enough.

Any ideas?

È stato utile?

Soluzione 2

Ok, it seems I found the problem. I put time.sleep(2) after os.remove(file_to_delete) and then it worked.

Altri suggerimenti

The file name string appear to be encoded as a unicode string.

Perhaps try this:

file_to_delete= unicodedata.normalize('NFKD', file_to_delete).encode('ascii','ignore')
os.remove(file_to_delete)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top