def sanitizePath(path): basename = os.path.basename(path) dirname = os.path.dirname(path) sanitizedName = sanitizeName(basename) if 0: print "path: " + path print "dirname: " + dirname print "basename: " + basename print "sanitizedName: " + sanitizedName print "renamed:", basename != sanitizedName if basename == sanitizedName: return path else: n = 1 s = sanitizedName index = s.rfind('.') fileTitle = sanitizedName if index != -1: fileTitle = s[:index] fileExtension = s.split(".")[-1] if fileExtension != sanitizedName: fileExtension = "." + fileExtension else: fileExtension = "" sanitizedName = dirname + "/" + fileTitle + fileExtension while os.path.exists(sanitizedName): sanitizedName = dirname + "/" + fileTitle + replacementChar + n.__str__() + fileExtension n+=1 rename(path, sanitizedName) return sanitizedName
def sanitizePath(path): basename = os.path.basename(path) dirname = os.path.dirname(path) sanitizedName = sanitizeName(basename) if False: print "path: " + path print "dirname: " + dirname print "basename: " + basename print "sanitizedName: " + sanitizedName print "renamed:", basename != sanitizedName if basename == sanitizedName: return path else: n = 1 s = sanitizedName index = s.rfind('.') fileTitle = sanitizedName if index != -1: fileTitle = s[:index] fileExtension = s.split(".")[-1] if fileExtension != sanitizedName: fileExtension = "." + fileExtension else: fileExtension = "" sanitizedName = dirname + "/" + fileTitle + fileExtension while os.path.exists(sanitizedName): sanitizedName = dirname + "/" + fileTitle + replacementChar + n.__str__( ) + fileExtension n += 1 rename(path, sanitizedName) return sanitizedName
def sanitizePath(path): basename = os.path.basename(path) dirname = os.path.dirname(path) sanitizedName = sanitizeName(basename) if basename == sanitizedName: return path else: n = 1 fileTitle, fileExtension = os.path.splitext(sanitizedName) sanitizedName = os.path.join(dirname, fileTitle + fileExtension) while os.path.exists(sanitizedName): sanitizedName = os.path.join(dirname, fileTitle + replacementChar + str(n) + fileExtension) n += 1 rename(path, sanitizedName) return sanitizedName
The name of files that are to be moved from one location to another is specified in a text file called 'Unsupported Files.txt' and the files that are meant to be moved are unsupported pdf files from the yesterday's work. The program should be executed once only after the successful execution as it will give error if the files and folders already exists or are moved to the location. INCOMPLETE FILE WORK IS YET TO BE DONE first check whether the pdf 's content name is null or not call for function name creater to get a proper name without any problems in the name if it is not null then only check whether the file name contain the term 'sector' in it, if so then only rename it """ import os import shutil fileobj = open('Unsupported Files.txt', 'r') contents = fileobj.read() contentlist = contents.split('\n') os.chdir('H:\\backup\\pdf') for file in contentlist: shutil.rename(file, 'H:\\backup\\Unsupported pdf\\') fileobj.close()
The name of files that are to be moved from one location to another is specified in a text file called 'Unsupported Files.txt' and the files that are meant to be moved are unsupported pdf files from the yesterday's work. The program should be executed once only after the successful execution as it will give error if the files and folders already exists or are moved to the location. INCOMPLETE FILE WORK IS YET TO BE DONE first check whether the pdf 's content name is null or not call for function name creater to get a proper name without any problems in the name if it is not null then only check whether the file name contain the term 'sector' in it, if so then only rename it """ import os import shutil fileobj = open('Unsupported Files.txt','r') contents = fileobj.read() contentlist = contents.split('\n') os.chdir('H:\\backup\\pdf') for file in contentlist: shutil.rename(file,'H:\\backup\\Unsupported pdf\\') fileobj.close()
def convert(self, target): ''' @summary: 元データの読み込みを行います ''' try: f = file(target, 'r') data = f.read() except IOError: print 'except: Cannot open "%s"' % target return False finally: f.close() data_enc = guessEncoding(data) data = data.decode(data_enc, 'strict') #バックアップファイルの作成 try: shutil.copyfile(target,target + '~') except: print u"ディレクトリにバックアップを作成できません。" return False #変換後のファイルを新規作成 try: f = file(target, 'w') except : print "%sを編集できません。" % target print 'except: Cannot open "%s"' % target return False #変換処理 try: for x,y in self.table: data = data.replace(x.decode(self.table_enc, 'strict'), y.decode(self.table_enc, 'strict')) f.write(data.encode(data_enc)) except: #変換中の例外はrollbackする try: shutil.rename(target + '~', target) except: print "%sが破損しました。バックアップファイルを参照してください。" % target raise finally: f.close()