目录和文件处理

#_*_ coding:utf-8 _*_
import os
import sys

path = 'G:\\迅雷下载\\ydma\\linux\\video'

filelist = list()
#https://blog.csdn.net/Wensent_H/article/details/77088623 os.system()有乱码的原因
#中间出现了这么几种意外:文件名中含有空格/文件名中含有&&/copy 原文件名中含有(1)/文件名中含有:
#G:\迅雷下载\ydma\linux\video\4用户组管理和进程管理\课时5:Linux服务管理&&计划任务中ts文件已直接修改为ts文件,执行时应检测不出,会重复新复制文件,不过每次会覆盖掉旧文件的

class Vedio_Combine:

    def __init__(self):
        self.list_expect_char = [':',':',' ','(',')','&']
        global filelist
        self.path = ''
        self.lists_f = filelist
        self.lists_d = list()
        self.os_command = False
        self.done = False

    def get_basename(self,fullpath):
        return os.path.basename(fullpath)

    def get_file(self,path_f):  #获得目录中的文件和目录做相应的处理

        if self.os_command:   #打开命令的开关
            os.chdir(path_f) #无返回值print就是NONE,执行相应目录的命令时要用
            commands = ''

        basename = self.get_basename(path_f)  #获得当前文件夹名,命令新文件视频时使用
        list_short = os.listdir(path_f)         #获得文件夹名或文件名
        for i in list_short:
            for j in self.list_expect_char:
                if j in i:
                    print('there is an ERROR,path name:',os.path.join(path_f,i))

        list_full = [os.path.join(path_f , i) for i in list_short] #获得全名,如果过滤成列表时要用

        for i in list_short:   #这里遍历当前文件夹,用短的,是因为后边要处理命令,用全名太长容易出错
            i_tofull = os.path.join(path_f,i)
            if os.path.isdir(i_tofull):
                self.get_file(i_tofull)
            elif i_tofull[-3:]== '.ts':   #如果是文件,且后缀名是.ts,则在当前目录下操作,要配合self.os_command开关
                self.done = True
                if self.os_command:
                    print('this has the py file##########################')
                    os.system('xcopy '+i_tofull+ ' '+os.sep.join(i_tofull.replace('ydma','ydmanew').split('\\')[0:-1])+'\\')
                else:
                    print("当前无法使用命令,未开启")
            else:   #过滤之外的文件,要做的处理,这里是将文件名连成字符串
                if self.os_command:
                    commands += i+ '+'
                else:
                    print('command is not support')
        else:   #当前目录下完全遍历时的操作
            if self.os_command:
                if commands !='':  #说明有过滤之外的文件,要操作

                    commands = 'copy /b '+commands[:-1]+ ' '+basename.replace(' ','')+'.ts'   #继续拼接命令
                    #os.system(commands)
                    if self.done :
                        pass
                        #print('already exists')
                    else:
                        if self.os_command:
                            os.system(commands + '&exit')
                            print('ok',commands,'ends')
                            print('command has been execued')
                            self.done =False
                        else:
                            print('command is not support')

                else:
                    print('out of the filter is no file to deal with')
            else:
                print('you don\'t need care!')


        self.ef(self.listfile(list_full))  #这里做的操作是将所有文件压入同一个列表,不进行区分所属文件夹
        #self.ed(self.listdir(list_full))  而这里做的操作是将所有文件夹压入同一个列表不进行区分所属文件夹




    def listdir(self):
        list = os.listdir(self.path)
        list_f = [self.path + i for i in list]
        print('\n'.join(list))

    def listfile(self, list):   #the item in list must be fullpath
        return [i for i in list if os.path.isfile(i)]

    def listdir(self, list):    #the item in list must be fullpath
        return [i for i in list if os.path.isdir(i)]

    def a(self, list):
        return self.lists_f.append(list)

    def ed(self, list):   #加入目录列表
        self.lists_d.extend(list)

    def ef(self,list):      #加入文件列表
        self.lists_f.extend(list)

vc = Vedio_Combine()
vc.get_file(path)
# for i in filelist:
#     print(i)

class judge:
    def __init__(self,val= True):
        self.i = val


    def j(self,fun,cue):  #判断的简写judge,i是判断的真值
        if self.i:
            fun
        else:
            print(cue)

猜你喜欢

转载自blog.csdn.net/wang880117/article/details/88047909