Difference between revisions of "Python Copying File and Directories"

From rbachwiki
Jump to navigation Jump to search
Line 1: Line 1:
==Move similar files to another folder==
'''Move liked files and open folder in explorer''
<pre>
import os
import shutil
source = ("C:\\Users\\bacchas\\Downloads\\")
sourcefiles = os.listdir(source)
dest = "D:\\karaoke_rename\\rename\\"
for file in sourcefiles:
    if file.endswith('.mp4'):
        shutil.move(os.path.join(source,file), os.path.join(dest,file))
os.startfile("D:\karaoke_rename")
os.system("python3 D:\\karaoke_rename\\rename\\rename_video_karaoke.py")
</pre>
= Rename Files: example1=
= Rename Files: example1=
<pre>
<pre>

Revision as of 15:43, 14 June 2023

Move similar files to another folder

'Move liked files and open folder in explorer

import os
import shutil
source = ("C:\\Users\\bacchas\\Downloads\\")
sourcefiles = os.listdir(source)
dest = "D:\\karaoke_rename\\rename\\"
for file in sourcefiles:
    if file.endswith('.mp4'):
        shutil.move(os.path.join(source,file), os.path.join(dest,file))


os.startfile("D:\karaoke_rename")
os.system("python3 D:\\karaoke_rename\\rename\\rename_video_karaoke.py")


Rename Files: example1

from datetime import datetime
from pathlib import Path
import os
## This script will rename files and move them into another folder
our_files = Path("C:\\Users\\bacchas\Desktop\youtubedl\\test")

for file in our_files.iterdir():
    if file.suffix != '.cdg' and file.is_file: #make sure its a file and not a directory
        directory = file.parent
        extension = file.suffix
        old_name = file.stem
        artist, title = old_name.split('-')
        artist = artist[4:].lstrip()
        new_name = f'{artist}-{title}{extension}'
       
        newpath_name = 'kmp3files'
        # will create a new path 
        new_path = our_files.joinpath(newpath_name)
        if not new_path.exists():
            new_path.mkdir()
        new_file_path = new_path.joinpath(new_name)
        file.rename(new_file_path)

       # print(new_name)
        print(new_path)

Rename Files: Example 2

import os
import re
#os.chdir('D:\\test\\rename')
i = input("enter starting value: ")
a = open('output.txt', 'w')
def video(i):
    for f in os.listdir():
        if f.endswith('.mp4'):
            print("--")
            f_name, f_ext = os.path.splitext(f)
            size = len(f)
            f_name = f_name.replace('(Video_Karaoke_with_a_black_background)', ' (Karaoke) ')
            f_name = f_name.replace('_', ' ')
            f_name = f_name[:- 8].strip()
            n_name = "{} ".format(i) + f_name + f_ext
            i = int(i) + 1
            os.rename(f, n_name)
            a.write(str(f_name) + os.linesep)
            print(n_name)

video(i)

Copying a single file

import shutil

src_path = r"C:/Users/jim/Dropbox/Filing Cabinet/Karaoke Stuff/newsongscode.txt"
dst_path = r"C:/Users/jim/Dropbox/All Folders/websites/msites/rb222/newsongscode.txt"
shutil.copy(src_path, dst_path)
print('copied')

Copy all files from a directory

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
   # construct full file path
   source = source_folder + file_name
   destination = destination_folder + file_name
   # copy only files
   if os.path.isfile(source):
       shutil.copy(source, destination)
       print('copied', file_name)

Copy Entire Directory

import shutil

source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree(source_dir, destination_dir)