Python Forum
Moving Files From Subdirectories To Another Directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving Files From Subdirectories To Another Directory
#1
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

Error:
Traceback (most recent call last): File "C:\Users\DELL\Desktop\Python_Projects\fonts-master\fonts-master\lel.py", line 30, in <module> shutil.move(path,r'C:\Users\DELL\Desktop\Python_Projects\fonts-master\All Fonts') File "C:\Users\DELL\AppData\Local\Programs\Python\Python38\lib\shutil.py", line 784, in move real_dst = os.path.join(dst, _basename(src)) File "C:\Users\DELL\AppData\Local\Programs\Python\Python38\lib\shutil.py", line 747, in _basename return os.path.basename(path.rstrip(sep)) AttributeError: 'WindowsPath' object has no attribute 'rstrip'
Reply
#2
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.
Reply
#3
(Oct-06-2020, 07:02 AM)bowlofred Wrote: 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.

I Have tried str(path)

But hey can you tell me how to move specific files from one dir to another dir
Reply
#4
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")
Reply
#5
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
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 434 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  change directory of save of python files akbarza 3 863 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,263 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,607 May-07-2023, 12:33 PM
Last Post: deanhystad
  Read directory listing of files and parse out the highest number? cubangt 5 2,316 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  How to save files in a separate directory Scordomaniac 3 1,825 Mar-16-2022, 10:17 AM
Last Post: Gribouillis
  Moving specific files then unzipping/decompressing christophereccles 2 2,349 Apr-24-2021, 04:25 AM
Last Post: ndc85430
  Moving files to Folders giddyhead 13 9,100 Mar-07-2021, 02:50 AM
Last Post: giddyhead
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,330 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  List of error codes to find (and count) in all files in a directory tester_V 8 3,648 Dec-11-2020, 07:07 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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