![]() |
Moving Files From Subdirectories To Another Directory - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Moving Files From Subdirectories To Another Directory (/thread-30121.html) |
Moving Files From Subdirectories To Another Directory - Harshil - Oct-06-2020 So Hey! I Have A Problem Not Homework Problem: I Have Many Font Files(Zip Download From GOogle Zip) And I Want to Move Them Only Ttf And Otf Files SO I have Done THis Approach #import libraries from pathlib import Path import os from datetime import datetime from collections import namedtuple import pandas as pd import shutil #Create A File nametuple file = namedtuple('File','name path size modified_date') #Create Empty list files = [] #File Path p = Path(r"C:\Users\DELL\Desktop\Python_Projects\fonts-master\fonts-master\ofl") #iterate though path objects find all that end with .ttf and .otf save to list for item in p.glob('**/*'): if item.suffix in ['.ttf','.otf']: name = item.name path = Path.resolve(item).parent size = item.stat().st_size modified = datetime.fromtimestamp(item.stat().st_mtime) files.append(file(name, path, size, modified)) #print(os.path.exists(str(path))) #ERROR BELOW shutil.move(path,r'C:\Users\DELL\Desktop\Python_Projects\fonts-master\All Fonts') #Create Dataframe from list df = pd.DataFrame(files) #Export To Csv df.to_csv('ttf_and_otf.csv',index=False)Here Are The Errors
RE: Moving Files From Subdirectories To Another Directory - bowlofred - Oct-06-2020 shutil.move() is expecting a string representing a filename or path, not a Path object. Most os file functions can take path-like objects, but I don't think shutil can.You can call it with str(path) instead of just path .
RE: Moving Files From Subdirectories To Another Directory - Harshil - Oct-06-2020 (Oct-06-2020, 07:02 AM)bowlofred Wrote: I Have tried str(path) But hey can you tell me how to move specific files from one dir to another dir RE: Moving Files From Subdirectories To Another Directory - bowlofred - Oct-06-2020 Can you show the error when you try? This code moves a "foo" file to the "target" directory. from pathlib import Path import shutil p = Path(".") for item in p.glob('**/*foo'): print(item) shutil.move(str(item), r"target") RE: Moving Files From Subdirectories To Another Directory - Harshil - Oct-06-2020 Hey! Thanks For The Help I Solved My Error Manually And I Want to Thank You! For Your Help I AM Able To Move Files! Here Is The Command I Used shutil.move(os.path.join(path,name),'Destination')But Is There A Way To Ignore errors In python RE: Moving Files From Subdirectories To Another Directory - ndc85430 - Oct-06-2020 Why would you want to ignore errors? They tell you that something's wrong and likely needs to be fixed for the program to work correctly. |