Hi all. I am new as can be to programming. I always heard a good way to learn to write was to find a project that I am interested in and start from there. So my ideal goal is to write a script that cleans up my downloads folder, ie moves .zips to the zip folder, pics to pics etc etc etc.

As you will be able to tell, I am not that far yet. I am still having problems getting the shutil.copy command to properly work; it keeps kicking out errors. I figured it was related to the path name as I am using a windows machine. So I tried to piece together some code using os.path commands etc etc.

Here is my code. I know this is an absolute mess extremely redundant and could probably be completed in two lines, but like I said.. I am completely green at this. Any help diagnosing my problem would be greatly appreciated.

import os
import shutil

print "This is the starting dir:", curdir

files= os.listdir(os.getcwd())

print "These are the files I want to copy:", files




print "This is the directory I want to copy to:",dest
print "This is where I am copying from:",curdir

shutil.copy(files, dest)

The errors I receive:
shutil.copy(files, dest)
File "C:\Python27\lib\shutil.py", line 115, in copy
dst = os.path.join(dst, os.path.basename(src))
File "C:\Python27\lib\ntpath.py", line 198, in basename
return split(p)[1]
File "C:\Python27\lib\ntpath.py", line 181, in split
return d + head, tail
TypeError: cannot concatenate 'str' and 'list' objects

Recommended Answers

All 5 Replies

The errors I receive:
shutil.copy(files, dest)

Print "files" and "dest" somewhere in the program, also type(files), type(dest);

TypeError: cannot concatenate 'str' and 'list' objects

to see which is not a string

After finding out which is not a string, is it possible to easily switch a list to a string? Or is there an module that I can use to take files from the testrun directory and move them to the 'updog' directory?

I tried to remove the os.listdir and just set files as os.getcwd(), hoping that this would get a path and I would be able to then use the shutil tocopy the files up one directory. Now I am getting a permission error.

I am more familiar with a unix environment and am at a loss as how to remedy a permission issue in a windows environment. I assume that I am missing something extremely simple as this seems to be a very simple problem (copying files up one dir).

Thanks for the help!

Read "Basics"in this page especially the use of a for() statement, for some help with using lists. shutil's copytree may or may not work as well.

Here is one short example how this sort of thing might be done:

# copy files with given file extensions from source to destination
# shutil.copy2() retains the last access time and modification time
# use shutil.move(src, dst) to move the file 

import os
import shutil

# assume you have created these destination folders
# you can use os.mkdir("C:/zz_zip") one time for instance
zip_folder = "C:/zz_zip"
pic_folder = "C:/zz_pic"
exe_folder = "C:/zz_exe"

# assume this is your download folder
download_folder = "C:/zz_download"

# loop through each file in the download folder
for fname in os.listdir(download_folder):
    # lower() takes care of any extension with upper case
    if fname.lower().endswith(".jpg"):
        source_file = os.path.join(download_folder, fname)
        destination_file = os.path.join(pic_folder, fname)
        shutil.copy2(source_file, destination_file)
    elif fname.lower().endswith(".zip"):
        source_file = os.path.join(download_folder, fname)
        destination_file = os.path.join(zip_folder, fname)
        shutil.copy2(source_file, destination_file)
    elif fname.lower().endswith(".exe"):
        source_file = os.path.join(download_folder, fname)
        destination_file = os.path.join(exe_folder, fname)
        shutil.copy2(source_file, destination_file)
    #print source_file, destination_file  # test

Nice project, but not easy by any means. If you experiment with the code, you can see how to shorten it some. Perhaps with some function to take care of repetitive code lines. One good exercise for you.

Thanks for the information, I will try to incorporate this into my script.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.