简体   繁体   中英

How to copy just the files in a folder in python3?

        source = ('C:\\Qualys Report\\Qualys Data\\')
        dest1 = ('C:\\Qualys Report\\Backup\\')           
        for filename in os.listdir(source):
            if filename.endswith('.csv'):
                shutil.move(source+filename, dest1)

For some reason its moving the folder and csv file i have into the backup folder

Anyway i can just move the file itself?

From the question it seems you are simply trying to copy the csv files from a single source dir (not recursively), you should use copy not move/rename if you wish to keep the original copy in place, with a copy in dest1.

    import os
    source = ('C:\\Qualys Report\\Qualys Data\\')
    dest1 = ('C:\\Qualys Report\\Backup\\')

    for filename in os.listdir(source):
        if filename.endswith('.csv'):
            shutil.copy(source+filename, dest1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM