DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Python delete file, invalid path

I want to delete a file located on my desktop:

os.remove('C:/Benutzer/Me/Desktop/sync.txt')

But I get

[Error 3] System cannot find the path

However the file does exist in the given location. I can copy the path and paste into explorer. This will open the file.

Where is the problem?

Upvotes: 1

Views: 3442

Answers (4)

Aamir Rind
Aamir Rind

Reputation: 39659

Are you sure the directory path is correct, if slashes causing problem (dont have to be) try this:

import os
filePath = 'C:' + os.path.sep + 'Benutzer' + os.path.sep + 'Me' + os.path.sep + 'Desktop' + os.path.sep + 'sync.txt'
os.remove(filePath)

the advantage of using os.path.sep here is that now you dont have to worry whether you are on linux or windows or whatever...

Upvotes: 1

joel goldstick
joel goldstick

Reputation: 4493

This looks like the correct answer. I googled and found this: link

Folder name (and path) in Windows XP Documents and Settings (C:\Documents and Settings)

In Vista and 7 it is moved to c:\Users

Upvotes: 0

glglgl
glglgl

Reputation: 91017

I suppose you are on Vista or 7? Then be aware of the UI to do quite a lot of localization.

Probably the path is really C:\Users\..., with the localization to Benutzer happening in the UI.

Upvotes: 4

etuardu
etuardu

Reputation: 5516

Try using backslashes instead of slashes, i.e. 'C:\Benutzer\Me\Desktop\sync.txt' (dos/windows style paths). To avoid the backslashes from being treated as escaping character use a raw string:

os.remove(r'C:\Benutzer\Me\Desktop\sync.txt')

Upvotes: 3

Related Questions