How to delete this file ???

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Dec 1 13:44:33 CET 2003


Gerrit Holl <gerrit at nl.linux.org> wrote in
news:mailman.1216.1070278795.702.python-list at python.org: 

> DCK wrote:
>> I've path to file, which look like this:
>> \\COMPUTER\D$\C++\FILE_TO_DELETE.JPG
> 
> What do you mean exactly by: 'looks like'? Is it the full path to the
> file? How did you find out?
> 
>> This path was generated by os.path.walk() function. When i try to
>> delete this file, os.remove() can't find it, os.path.fileexists()
>> can't find it :( 
> 
> Does the directory exist, e.g., what does
> 'os.path.exists('\\computer\d$\c++') give as result? And
> 'os.path.exists('\\computer\d$')? I think a 'd$' is usually a share in
> Windows (not sure), maybe you haven't turned the share on (I'm not
> sure what 'mounting' is called in Windows)?

You need some raw string quoting there.

os.path.exists(r'\\computer\d$\c++') would check for the existence of a 
directory c++ on drive D of the computer named 'computer', provided you 
have administrative access to the remote computer. The drive letter shares 
c$ (and d$ etc. where there is more than one drive) are created 
automatically and are only accessible to administrators. The UNC pathnames 
being used mean that there is no need to mount the drive before accessing 
it.

os.path.exists(r'\\computer\d$') will return False whether or not the share 
exists, however os.path.exists(r'\\computer\d$\\') will return True if the 
share exists. Note that you need two trailing backslashes even if you don't 
use the raw quoting.

I tried creating a file called FILE_TO_DELETE.JPG in a directory called C++ 
on a remote machine. I checked the existence of both the file and directory 
using os.path.exists, and then removed first the file and then the 
directory using os.remove. Both operations worked as expected, so the '+' 
character in the filename is not a problem (Python 2.3). I suspect the OP 
must have something else wrong, possibly a confusion over backslashes, or 
possibly just insufficient access to the C++ directory (just because you 
have access to the admin shares doesn't mean you necessarily have full 
access to all the directories).

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list