[Tutor] using os module on windows

Kent Johnson kent37 at tds.net
Wed Feb 20 14:42:54 CET 2008


Timmie wrote:

> ###### config ######
> source = 'C:/my/local/directory'
> dest = 'Z:/my/network/drive'
> ## start action
> #os.remove(dest)
> 
> ## error output
> Traceback (most recent call last):
>   File "test_os_on_win", line 14, in <module>
>     os.remove(dest)
> WindowsError: [Error 5] Zugriff verweigert: 'Z:/my/network/drive'

os.remove() removes files, is Z:/my/network/drive a file or a directory?
os.rmdir() removes *empty* directories IIRC.
shutir.rmtree() will remove an entire directory tree and the files in it.

> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
> import shutil
> import os
> 
> ###### config ######
> source = 'C:/my/local/directory'
> dest = 'Z:/my/network/drive'
> ## start action
> 
> shutil.copytree(source, dest)
> 
> Traceback (most recent call last):
>   File "test_os_on_win", line 17, in <module>
>     shutil.copytree(source, dest)
>   File "C:\python\lib\shutil.py", line 110, in copytree
>     os.makedirs(dst)
>   File "C:\python\lib\os.py", line 172, in makedirs
>     mkdir(name, mode)
> WindowsError: [Error 183] Eine Datei kann nicht erstellt werden, wenn sie
> bereits vorhanden ist: 'Z:/my/network/drive'

The destination path should not already exist, it should end with the 
directory you want to create. Did you read the docs for copytree()? They 
say, "The destination directory, named by dst, must not already exist; 
it will be created as well as missing parent directories."

Try shutil.copytree(source, os.path.join(dest, 'copy_of_c'))

Kent


More information about the Tutor mailing list