Python Copying File and Directories

From rbachwiki
Revision as of 16:27, 14 June 2023 by Bacchas (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Rename Karaoke Video Files script

import os
import re
os.chdir('D:\\karaoke_rename\\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(n_name) + os.linesep)
            print(n_name)

video(i)

Move similar files to another folder

'Move liked files and open folder in explorer

import os
import shutil
source = "C:\\Users\\sales\\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")

Cleanup download folder by moving similar files to assigned folder

import os
import shutil
import fnmatch

src = "C:\\Users\\sales\\Downloads\\"
os.chdir(src)

ftype = ["*.pdf", "*.exe",
         "*.txt","*.jpg",
         "*.png","*.xlsx",
         "*.xls","*.docx",
         "*.doc","*.ppt",
         "*.pptx","*.zip",
         "*.jpeg","*.iso"]
dest = [src + "pdf",     src + "exe",
        src + "txt",     src + "images",
        src + "images",  src + "office",
        src + "office",  src + "office",
        src + "office",  src + "office",
        src + "office",  src + "zip",
        src + "images",  src + "iso"]

files = os.listdir(src)
print(files)
def clean():
     for ftype1, dest1 in zip(ftype,dest):
          for f in files:
               if fnmatch.fnmatch(f,ftype1)==True:
                    full_file_name = os.path.join(src,f)
                    if os.path.isfile(full_file_name):
                         shutil.move(full_file_name, dest1)
                         print("[",full_file_name,"]",  " - " ,"[" ,dest1,"]")
       
clean()


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\\sales\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)