1

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?

asked Sep 7, 2011 at 11:00
3
  • Are you sure, that the '/' are correct? Remember, that on windows you need to use '\'. Commented Sep 7, 2011 at 11:06
  • 2
    Error 3 is path not found rather than file not found. Something's up with the path. Commented Sep 7, 2011 at 11:09
  • try os.path.exists('C:/Benutzer/Me/Desktop/sync.txt') Commented Sep 7, 2011 at 11:18

4 Answers 4

4

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.

answered Sep 7, 2011 at 11:40

1 Comment

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

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')
answered Sep 7, 2011 at 11:05

2 Comments

This won't be it since a / is a perfectly valid path separator on windows.
isn't / is platform independent??
1

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...

answered Sep 7, 2011 at 12:59

1 Comment

Your are principially right, but this solution looks ugly. A semantically equivalent, but better solution is filePath = os.path.join('C:', 'Benutzer', 'Me', 'Desktop', 'sync.txt').
0

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

answered Sep 7, 2011 at 12:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.