Skip to content
Advertisement

How to detect folder path i ran script for?

the below script is to unzip zip file and rename extaracted subtitles names according to tv show episodes names. and then convert them to utf-8.

here is the problem:

I want to run this script in a linux os and inside any tv show folder i want.
but I want the path in fixing function to be detected from the folder itselt i run the python script for because it is not a constant path, there are many tv show folders.

sorry for my english

import zipfile
import os
import re
from chardet import detect
import fnmatch

def find(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name))
    return result



def get_encoding_type(file):
    with open(file, 'rb') as f:
        rawdata = f.read()
    return detect(rawdata)['encoding']

def correctSubtitleEncoding(filename, newFilename, encoding_from, encoding_to='UTF-8'):
    with open(filename, 'r', encoding=encoding_from) as fr:
        with open(newFilename, 'w', encoding=encoding_to) as fw:
            for line in fr:
                fw.write(line[:-1]+'rn')

def fixing(path):

    oldsrtfiles = [f for f in os.listdir(path) if '.srt' in f or '.ass' in f ]
    print(len(oldsrtfiles), ' old subtitles found')
    if len(oldsrtfiles) != 0:
        for oldsrt in oldsrtfiles:
            os.remove(f'{path}{oldsrt}')
            print(f'{oldsrt} removed')

    filename = find('*.zip', path)[0]
    with zipfile.ZipFile(f'{filename}',"r") as zip_ref:
        zip_ref.extractall(path)
        print('files extarcted')

    os.remove(f'{filename}')
    print("Zip File Removed!")

    newsrtFiles = [f for f in os.listdir(path) if '.srt' in f or '.ass' in f ]
    print(len(newsrtFiles), ' subtitles found')


    showsTitles = [f for f in os.listdir(path) if '.mkv' in f or '.avi' in f or '.mp4' in f]
    print(len(showsTitles), ' tv show found')

    pattern = r'S(d{1,2})E(d{1,2})'

    for show in showsTitles:
        SEneeded = re.search(pattern, show).group(0)
        for i, sub in enumerate(newsrtFiles):
            if SEneeded in sub:
                if sub[-3:] == 'srt':
                    newsrtFiles[i] = show.replace(show[-3:],'ar.srt')
                    os.rename(f'{path}{sub}',f'{path}{newsrtFiles[i]}')
                elif sub[-3:] == 'ass':
                    subs[i] = show.replace(show[-3:],'ar.ass')

    forencoding = [f for f in os.listdir(path) if '.srt' in f or '.ass' in f ]
    for newsrtfile in forencoding:
        from_codec = get_encoding_type(f'{path}{newsrtfile}')
        print(from_codec)
        correctSubtitleEncoding(f'{path}{newsrtfile}', f'{path}{newsrtfile}', from_codec, encoding_to='UTF-8')

Advertisement

Answer

function to get the current working directory

os.getcwd()

to call this function you need to import os module

import os
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement