Python Forum
how to move files to another folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to move files to another folder
#1
Hello,

I would like to move specific files (that have "*CT*" in filenames) from one folder to another new folder
I tried to do that with shutil but it doesnt work in my case.
I can't move files to ct directory. It created me a new file with the name "ct".

ctDir = str("ct")
petDir = str("pet")
os.mkdir(ctDir)
os.mkdir(petDir)

inputPath = "C:\\data\\test2\\"

print("PET files")
os.chdir(inputPath)
for file in glob.glob("*PT*"):
    print(file)
    petDirMove = str(petDir) + "\\" + file
    shutil.move(file,petDirMove)

print("CT files")
os.chdir(inputPath)
for file in glob.glob("*CT*"):
    print(file)
    ctDirMove = str(ctDir) + "\\" + file   
    shutil.move(file, ctDirMove)
I would be appreciate for any help please.
Reply
#2
Can you be more specific about how it is not working? Are you getting an error or are the files just not moving.

Some things I see that are probably not the problem: Don't use file for a variable name. That word already has meaning in Python, and by using it as a variable name you change it's meaning, for your code and possibly shutil's code. I would use os.path.join to calculate petDirMove and ctDirMove. Note that str("pet") is equivalent to "pet" so you get rid of most if not all of those calls to str.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 552 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Move Files based on partial Match mohamedsalih12 2 829 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Rename files in a folder named using windows explorer hitoxman 3 747 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,497 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  python move specific files from source to destination including duplicates mg24 3 1,103 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  How to loop through all excel files and sheets in folder jadelola 1 4,503 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 4,025 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,608 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge all json files in folder after filtering deneme2 10 2,367 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,503 Dec-18-2021, 09:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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