shutil module

Harvey Thomas hst at empolis.co.uk
Mon Nov 18 05:56:59 EST 2002


asdfasd asdfasdf  wrote
>hi all
>
>i wanted to copy a file to another directory so i used the shutil module's copy() function. The name of this file will >>change everyday.eg, events-18-Nov-2002.txt and tomorrow will be events-19-Nov-2002.txt
>
>i used:
>import shutil
>import glob
>shutil.copy (glob.glob(r'c:\winnt\temp\events*'), r'c:\temp')
>
>i couldn't seem to copy the file to c:\temp..it says
>Traceback (most recent call last):
>  File "<pyshell#132>", line 1, in ?
>    shutil.copy(glob.glob(r'c:\winnt\temp\events-*'),r'c:\temp')
>  File "C:\Python22\lib\shutil.py", line 61, in copy
>    dst = os.path.join(dst, os.path.basename(src))
>  File "C:\Python22\Lib\ntpath.py", line 190, in basename
>    return split(p)[1]
>  File "C:\Python22\Lib\ntpath.py", line 149, in split
>    while i and p[i-1] not in '/\\':
>TypeError: 'in <string>' requires character as left operand
>
>i checked the output of glob and it says
>>>> glob.glob(r'c:\winnt\temp\events-*')
>['c:\\winnt\\temp\\events-18-Nov-2002.txt']
>
>could it be the double slash that is making the fault....??
>
>thanks for anyhelp

The arguments to shutil.copy are strings - the pathnames of the source and destination files. glob.glob, however, returns a list of strings, as you can see above where you have a list containing one string -
['c:\\winnt\\temp\\events-18-Nov-2002.txt'].

So, try (untested)

for src in glob.glob(r'c:\winnt\temp\events*'):
    shutil.copy (src, r'c:\temp')

HTH

Harvey

_____________________________________________________________________
This message has been checked for all known viruses by the MessageLabs Virus Scanning Service.




More information about the Python-list mailing list