Python Forum
I am trying to copy files into a bunch of folders.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am trying to copy files into a bunch of folders.
#1
I have 2 types of file types in a folder .docx/.doc and .pptx/.ppt, I have written a python script to cut and paste them into 2 folders one documents and the other powerpoints.

import os
FileTypeList = [".doc",".ppt"]
dirList = os.listdir(os.getcwd())
for FileType in FileTypeList:
    for item in dirList:
        if FileType in item:
            print(True)
        else:
            print(False)
It always prints False. When I try:

if ".doc" in "document.docx":
    print("True")
else:
    print("False")
This prints True.
How can this be fixed?
Reply
#2
from glob import glob

file_exts = ['*.doc', '*.ppt']

for ext in file_ext:
    print(glob(ext))
glob.glob(pattern) will return a list of matched file names
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
FileTypeList is a bad variable name.
Look at name wavic use file_exts,it's with underscore no CamelCase.
PEP 8.
Reply
#4
Your code works fine for me. I switched it to .txt or .py, and in prints a bunch of Trues and Falses.

It is rather inefficient. You are checking each file twice, once to see if it's a Word doc, once to see if it's a PowerPoint file. It would be better to make a dictionary with file extensions as the keys, and folders they should be copied to as the values. Also, I would check with endswith rather than in. It's more precise and (again) more efficient.
import os
FileTypes = {'.txt': 'Text file','.py': 'Python program', '.xml': 'XML data'}
dirList = os.listdir(os.getcwd())
for item in dirList:
    for extension, folder in FileTypes.items():
        if item.endswith(extension):
            print(folder)
            break
    else:
        print('Other')
See how I used break? It only checks until it finds something, then it stops checking the other extensions. We can also use the else clause to catch anything to didn't match the extensions we were looking for.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I made a very minor error. 
FileTypeList was actually coming from a file. Because it had \n at the end of the string the if statement returned false.
Now i did:
if FileType.rstrip() in item:
    Blah
This returns true.

I did not post that.  Wall My mistake. Sorry.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 282 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 287 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  Create new folders and copy files cocobolli 3 1,481 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,021 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  a bunch of modules to import Skaperen 2 903 Nov-07-2022, 07:33 PM
Last Post: Gribouillis
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,509 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Moving files to Folders giddyhead 13 9,192 Mar-07-2021, 02:50 AM
Last Post: giddyhead
  How to use Bunch data structure moish 2 2,923 Dec-24-2020, 06:25 PM
Last Post: deanhystad
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,871 Dec-06-2020, 04:14 PM
Last Post: Divya577

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020