def genPeverifyInteropAsm(file): #if this isn't a test run that will invoke peverify there's no point in #continuing if not is_peverify_run: return else: mod_name = file.rsplit("\\", 1)[1].split(".py")[0] print "Generating interop assemblies for the", mod_name, "test module which are needed in %TEMP% by peverify..." from System.IO import Path tempDir = Path.GetTempPath() cwd = nt.getcwd() #maps COM interop test module names to a list of DLLs module_dll_dict = { "excel": [], "msagent": [agentsvr_path], "scriptpw": [scriptpw_path], "word": [], } dlrcomlib_list = [ "dlrcomserver", "paramsinretval", "method", "obj", "prop", ] if is_cli32: temp_name = testpath.rowan_root + "\\Test\\DlrComLibrary\\Debug\\DlrComLibrary.dll" else: temp_name = testpath.rowan_root + "\\Test\\DlrComLibrary\\x64\\Release\\DlrComLibrary.dll" for mod_name in dlrcomlib_list: module_dll_dict[mod_name] = [temp_name] if not file_exists_in_path("tlbimp.exe"): print "ERROR: tlbimp.exe is not in the path!" sys.exit(1) try: if not module_dll_dict.has_key(mod_name): print "WARNING: cannot determine which interop assemblies to install!" print " This may affect peverify runs adversely." print return else: nt.chdir(tempDir) for com_dll in module_dll_dict[mod_name]: if not file_exists(com_dll): print "\tERROR: %s does not exist!" % (com_dll) continue print "\trunning tlbimp on", com_dll run_tlbimp(com_dll) finally: nt.chdir(cwd)
def change_dir(self): file_name = os.listdir(self.path) print(file_name) for files in file_name: file = os.path.splitext(files) print(file) if self.old_name in file[0]: new_name = file[0].replace(self.old_name, self.new_name) + file[1] chdir(self.path) #在使用rename之前需要进入文件所在的目录 os.rename(files, new_name)
def removeNumbers(dir): chdir(dir) files = os.listdir(dir) # files.sort() files.sort(lambda x, y: compare_string(x, y)) for old_file in files: if 'py' not in old_file: fileName = str(old_file) new_name = removeDigitPrefix(fileName) new_name = removeDotPrefix(new_name) print "old file: ", old_file, "=> new file: ", new_name os.rename(old_file, new_name)
def addNumbers(dir, start): i = start chdir(dir) files = os.listdir(dir) # files.sort() files.sort(lambda x, y: compare_string(x, y)) for old_file in files: fileName = str(old_file) new_name = str(i) + '.' + fileName os.rename(old_file, new_name) print "remamed: ", old_file, " ==> ", new_name i += 1
def genPeverifyInteropAsm(file): #if this isn't a test run that will invoke peverify there's no point in #continuing if not is_peverify_run: return else: mod_name = file.rsplit("\\", 1)[1].split(".esc")[0] print("Generating interop assemblies for the", mod_name, "test module which are needed in %TEMP% by peverify...") from System.IO import Path tempDir = Path.GetTempPath() cwd = nt.getcwd() #maps COM interop test module names to a list of DLLs module_dll_dict = { "excel" : [], "msagent" : [agentsvr_path], "scriptpw" : [scriptpw_path], "word" : [], } dlrcomlib_list = [ "dlrcomserver", "paramsinretval", "method", "obj", "prop", ] if is_cli32: temp_name = testpath.rowan_root + "\\Test\\DlrComLibrary\\Debug\\DlrComLibrary.dll" else: temp_name = testpath.rowan_root + "\\Test\\DlrComLibrary\\x64\\Release\\DlrComLibrary.dll" for mod_name in dlrcomlib_list: module_dll_dict[mod_name] = [ temp_name ] if not file_exists_in_path("tlbimp.exe"): print("ERROR: tlbimp.exe is not in the path!") sys.exit(1) try: if mod_name not in module_dll_dict: print("WARNING: cannot determine which interop assemblies to install!") print(" This may affect peverify runs adversely.") print() return else: nt.chdir(tempDir) for com_dll in module_dll_dict[mod_name]: if not file_exists(com_dll): print("\tERROR: %s does not exist!" % (com_dll)) continue print("\trunning tlbimp on", com_dll) run_tlbimp(com_dll) finally: nt.chdir(cwd)
def main(cpy_dir): global CPY_DIR global CPY_LIB_DIR CPY_DIR = cpy_dir CPY_LIB_DIR = cpy_dir + "\\Lib" #E.g., C:\Python25\Lib path.append(CPY_LIB_DIR) path.insert(0, ".") print( "------------------------------------------------------------------------" ) print("--PACKAGES") cwd = nt.getcwd() nt.chdir(CPY_LIB_DIR) for pack_name in nt.listdir(CPY_LIB_DIR): if not is_package(pack_name): continue elif pack_name in BLACKLIST: continue check_package(pack_name) print() nt.chdir(cwd) print( "------------------------------------------------------------------------" ) print("--MODULES") for x in nt.listdir(CPY_LIB_DIR): if x.endswith(".py"): mod_name = x.split(".py", 1)[0] else: continue try: import_helper(mod_name) log_ok(mod_name) except Exception as e: log_broken(mod_name, e) #Cleanup for f in FILE_LIST: f.close() return BROKEN_LIST
def rename(): filelist = os.listdir(path) # 该文件夹下的所有文件 for file in filelist: # 遍历所有文件 包括文件夹 # print("原始文件名为 :%s"%file) index = file.rfind(olds) # print(index) if index > 0: newName = file.replace(olds, news) # newName=file[index+3:] print("新文件名为 :%s" % newName) # 先用chdir()函数进入到目标文件所在的路径 chdir(os.path.dirname(path + file)) # 如果不是文件夹,需要添加此语句 os.rename(file, newName) # 重命名
def removeNumbers0(dir): chdir(dir) for old_file in os.listdir(dir): if 'py' not in old_file: fileName = str(old_file) number = fileName[0:fileName.find('.')] print "number: ", number if (isNumber2(number)): subfilename = fileName[fileName.find('.') + 1:] fileName = subfilename[subfilename.find('.'):] new_name = subfilename print "old file: ", old_file print "subfilename: ", subfilename print "new_name: ", new_name os.rename(old_file, new_name) else: print "number: ", number, "is not number"
def check_package(package_name): ''' Checks all subpackages and modules in the package_name package. ''' cwd = nt.getcwd() if cwd == CPY_LIB_DIR: root_name = package_name else: root_name = cwd.split(CPY_DIR + "\\Lib\\")[1].replace( "\\", ".") + "." + package_name #First check that the root package can be imported try: import_helper(package_name) log_ok(root_name) except (Exception, SystemExit) as e: log_broken(root_name, e) # no sense continuing return #Next examine subpackages and modules nt.chdir(cwd + "\\" + package_name) for x in nt.listdir("."): if x.endswith(".py") and x not in ("__init__.py", '__main__.py'): x = x.split(".py", 1)[0] mod_name = nt.getcwd().split(CPY_DIR + "\\Lib\\")[1] + "\\" + x mod_name = mod_name.replace("\\", ".") try: import_helper(mod_name) log_ok(mod_name) except (Exception, SystemExit) as e: log_broken(mod_name, e) elif is_package(x) and not x.startswith('test'): check_package(x) nt.chdir(cwd)
def main(cpy_dir): global CPY_DIR global CPY_LIB_DIR CPY_DIR = cpy_dir CPY_LIB_DIR = cpy_dir + "\\Lib" #E.g., C:\Python25\Lib path.append(CPY_LIB_DIR) path.insert(0, ".") print "------------------------------------------------------------------------" print "--PACKAGES" cwd = nt.getcwd() nt.chdir(CPY_LIB_DIR) for pack_name in nt.listdir(CPY_LIB_DIR): if not is_package(pack_name): continue #Importing all tests takes too long elif pack_name in ["test"]: continue check_package(pack_name) print nt.chdir(cwd) print "------------------------------------------------------------------------" print "--MODULES" for x in nt.listdir(CPY_LIB_DIR): if x.endswith(".py"): mod_name = x.split(".py", 1)[0] else: continue try: import_helper(mod_name) log_ok(mod_name) except Exception, e: log_broken(mod_name, e)
def download(self, imglist): for imgurl in imglist: print "正在下载第" + str(self.jpgx) + "图片" #传回进度信号 self.progress.emit([self.jpgx, self.cPhoto, self.x]) #print str(self.addr) #中文地址加编码utf-8 path = unicode(self.addr, "utf8") chdir(str(path)) proxy_handler = urllib2.ProxyHandler({'http': '127.0.0.1:11211'}) opener = urllib2.build_opener(proxy_handler) f = opener.open(imgurl) data = f.read() with open(str(self.jpgx) + '.jpg', 'wb') as code: code.write(data) #urllib.urlretrieve(imgurl,'%s.jpg' % x) self.jpgx += 1 print "download"
def test_chdir(): currdir = nt.getcwd() nt.mkdir('tsd') nt.chdir('tsd') AreEqual(currdir + '\\tsd', nt.getcwd()) nt.chdir(currdir) AreEqual(currdir, nt.getcwd()) nt.rmdir('tsd') # the directory is empty or does not exist AssertErrorWithNumber(WindowsError, 22, lambda: nt.chdir('')) AssertErrorWithNumber(WindowsError, 2, lambda: nt.chdir('tsd'))
def test_chdir(): currdir = nt.getcwd() nt.mkdir('tsd') nt.chdir('tsd') AreEqual(currdir+'\\tsd', nt.getcwd()) nt.chdir(currdir) AreEqual(currdir, nt.getcwd()) nt.rmdir('tsd') # the directory is empty or does not exist AssertErrorWithNumber(WindowsError, 22, lambda:nt.chdir('')) AssertErrorWithNumber(WindowsError, 2, lambda:nt.chdir('tsd'))
def test_chdir(self): currdir = nt.getcwd() nt.mkdir('tsd') nt.chdir('tsd') self.assertEqual(os.path.join(currdir, 'tsd'), nt.getcwd()) nt.chdir(currdir) self.assertEqual(currdir, nt.getcwd()) nt.rmdir('tsd') # the directory is empty or does not exist self.assertRaisesNumber(WindowsError, 22, lambda:nt.chdir('')) self.assertRaisesNumber(WindowsError, 2, lambda:nt.chdir('tsd'))
def test_chdir(self): currdir = nt.getcwd() nt.mkdir('tsd') nt.chdir('tsd') self.assertEqual(os.path.join(currdir, 'tsd'), nt.getcwd()) nt.chdir(currdir) self.assertEqual(currdir, nt.getcwd()) nt.rmdir('tsd') # the directory is empty or does not exist self.assertRaisesNumber(WindowsError, 22, lambda: nt.chdir('')) self.assertRaisesNumber(WindowsError, 2, lambda: nt.chdir('tsd'))
nudetokens = [] for token in tokens: token = token.strip(u'.,،)(:') nudetokens.append(token) result = [x for x in nudetokens if x not in newwords and len(x) > 2 and x!=match] # for i in result: # print i return result ##file=codecs.open('d:\lion.txt','r','utf-8') ##file2=codecs.open('d:\milk.txt','r','utf-8') filenames = glob.glob('d:\\wsd\\*.txt') nt.chdir('d:\\wsd') feat_set = [] sets = [] for name in filenames: # print name filename = name.split('\\')[-1] sense = name.split('\\')[-1].split('.')[0] print 'training', sense file = codecs.open(filename, 'r', 'utf-8') text = file.read() features = extract_words(text) # # for item in sorted(features): # print item
import os from nt import chdir path = r'C:\Users\Dell\Desktop\0' + '\\man\\' + str(1) print path aim = r'C:\Users\Dell\Desktop\0' + '\\man\\' + str(2) files = os.listdir(path) i = 0 for f in files: print f chdir(os.path.dirname(path + '\\' + f)) os.rename(f, aim + '\\' + str(i) + '.jpg') i = i + 1
if cwd==CPY_LIB_DIR: root_name = package_name else: root_name = cwd.split(CPY_DIR + "\\Lib\\")[1].replace("\\", ".") + "." + package_name #First check that the root package can be imported try: import_helper(package_name) log_ok(root_name) except (Exception, SystemExit), e: log_broken(root_name, e) #Next examine subpackages and modules nt.chdir(cwd + "\\" + package_name) for x in nt.listdir("."): if x.endswith(".py") and x!="__init__.py": x = x.split(".py", 1)[0] mod_name = nt.getcwd().split(CPY_DIR + "\\Lib\\")[1] + "\\" + x mod_name = mod_name.replace("\\", ".") try: import_helper(x) log_ok(mod_name) except (Exception, SystemExit), e: log_broken(mod_name, e) elif is_package(x):
#--HELPER FUNCTIONS------------------------------------------------------------ def ip_passes(mod_name): print mod_name IPY_PASSES.write(mod_name + "\n") IPY_PASSES.flush() def ip_fails(mod_name): IPY_FAILS.write(mod_name + "\n") IPY_FAILS.flush() #--MAIN----------------------------------------------------------------------- nt.chdir(CPY_DIR + r"\Lib") for mod_name in TEST_LIST: proc = Process() proc.StartInfo.FileName = sys.executable proc.StartInfo.Arguments = "test\\" + mod_name proc.StartInfo.UseShellExecute = False proc.StartInfo.RedirectStandardOutput = True if (not proc.Start()): raise Exception("Python process failed to start: " + mod_name) else: cpymod_dir = proc.StandardOutput.ReadToEnd() if not proc.HasExited: raise Exception("Python process should have exited by now: " + mod_name)
def renameF(preName, newName): chdir(os.path.dirname(preName)) os.rename(preName, newName)
IPY_PASSES = open("IPY_PASSES.log", "w") #Log containing all tests IP fails IPY_FAILS = open("IPY_FAILS.log", "w") #--HELPER FUNCTIONS------------------------------------------------------------ def ip_passes(mod_name): print mod_name IPY_PASSES.write(mod_name + "\n") IPY_PASSES.flush() def ip_fails(mod_name): IPY_FAILS.write(mod_name + "\n") IPY_FAILS.flush() #--MAIN----------------------------------------------------------------------- nt.chdir(CPY_DIR + r"\Lib") for mod_name in TEST_LIST: proc = Process() proc.StartInfo.FileName = sys.executable proc.StartInfo.Arguments = "test\\" + mod_name proc.StartInfo.UseShellExecute = False proc.StartInfo.RedirectStandardOutput = True if (not proc.Start()): raise Exception("Python process failed to start: " + mod_name) else: cpymod_dir = proc.StandardOutput.ReadToEnd() if not proc.HasExited: raise Exception("Python process should have exited by now: " + mod_name)
import os import sys from nt import chdir #path = os.path.dirname(os.path.abspath(__file__)) path = sys.argv[1] print path chdir(path) fileList = os.listdir(path) print fileList for i in fileList: if (i.find('GH') == 0): newname = 'GH' + i[4:8] + i[2:4] + '.MP4' print newname os.rename(i, newname)
import os,re _1path="." from nt import chdir chdir(_1path) for item in os.listdir(_1path): _1Re=re.match("^.*?([0-9a-z\-\_]+).*?(\.mp4|\.mts)",item,re.I) if _1Re: newname=_1Re.group(1)+_1Re.group(2) os.rename(item,newname) print(item+"-->"+newname) input()
#完善发件人收件人,主题信息 message = MIMEMultipart() message['From'] = formataddr(["姚佳龙", sender]) message['To'] = formataddr(["wqx", ''.join(receivers)]) subject = '测试报告' message['Subject'] = Header(subject, 'utf-8') #正文部分 textmessage = MIMEText('', 'html', 'utf-8') message.attach(textmessage) workLoc = os.path.join('E:\\', 'pythonProject', 'interface') # #检查路径有效性 if (os.path.exists(workLoc)) & (os.path.isdir(workLoc)): #尝试改变当前工作路径: chdir(workLoc) else: print('路径无效,请从新检查') sys.exit() #尝试添加附件 File = '报告.html' print("附件文件名为:%s" % File) FileLoc = os.path.join(workLoc, File) FileAtt = MIMEApplication(open(FileLoc, 'rb').read()) FileAtt.add_header('Content-Disposition', 'attachment', filename=File) message.attach(FileAtt) #发送邮件操作 smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit()
sys.exit(1) # Try to import a common module that *should* work. print "Looking for Python root directory..." while 1: pwd = nt.getcwd() ##print "Could it be", `pwd`, "?" try: open("Lib\\os.py").close() ##print "It appears so." break except IOError: ##print "Hm, it doesn't appear to be. Try the parent directory." try: opwd = pwd nt.chdir("..") pwd = nt.getcwd() if opwd == pwd: ##print "Seems like we're in the root already." raise nt.error except nt.error: ##print "Can't chdir to the parent -- we're stuck." pass else: ##print "Try again one level higher." continue print "Hey, would you like to help?" print "Please enter the pathname of the Python root." while 1: try: dirname = raw_input("Python root: ")
def parse(self,response): browser=webdriver.Chrome("D:/tools/chromedriver.exe") browser.get(response.url) s1=Select(browser.find_element_by_xpath("//*[@id='ctl00_MainContent_lbCommodity']")) for select in s1.options: s1.select_by_visible_text(select.text) excel=browser.find_element_by_xpath("//*[@id='ctl00_MainContent_rblOutputType_2']") excel.click() startdate=browser.find_element_by_xpath("//*[@id='ctl00_MainContent_tbStartDate']") enddate=browser.find_element_by_xpath("//*[@id='ctl00_MainContent_tbEndDate']") chdir(os.path.dirname("D:/spider/fasusda/ExportSalesDataByCommodity.xls")) filetime=time.localtime(os.stat("D:/spider/fasusda/ExportSalesDataByCommodity.xls").st_ctime) os.remove("D:/spider/fasusda/ExportSalesDataByCommodity.xls") print filetime date=[str(filetime.tm_mon),str(filetime.tm_mday),str(filetime.tm_year)] date=["12","9","2016"] localtime=time.strftime("%Y%m%d",time.localtime(time.time())) if int(date[0])<10: date[0]='0'+date[0] if int(date[1])<10: date[1]='0'+date[1] print date startdate.clear() startdate.send_keys(date[0]+"/"+date[1]+"/"+date[2]) enddate.clear() print localtime[4:6]+"/"+localtime[6:]+"/"+localtime[:4] enddate.send_keys(localtime[4:6]+"/"+localtime[6:]+"/"+localtime[:4]) time.sleep(20) b=29 if date[0]=='01' or date[0]=='03' or date[0]=='05' or date[0]=='07' or date[0]=='08' or date[0]=='10': if int(date[1])+7>31: temp=int(date[1])+7-31 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) a=int(date[0])+1 if a<10: date[0]="0"+str(a) else: date[0]=str(a) else: temp=int(date[1])+7 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) elif date[0]=='04' or date[0]=='06' or date[0]=='09' or date[0]=='11': if int(date[1])+7>30: temp=int(date[1])+7-30 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) a=int(date[0])+1 if a<10: date[0]="0"+str(a) else: date[0]=str(a) else: temp=int(date[1])+7 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) elif date[0]=='12': if int(date[1])+7>31: temp=int(date[1])+7-31 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) a=int(date[2])+1 date[0]='01' date[2]=str(a) else: temp=int(date[1])+7 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) elif date[0]=='02': year=int(date[2]) if (year%4==0 and year%100!=0) or year%400==0: b=29 else: b=28 if int(date[1])+7>b: temp=int(date[1])+7-b if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) date[0]='03' else: temp=int(date[1])+7 if temp<10: date[1]='0'+str(temp) else: date[1]=str(temp) #enddate.clear() #enddate.send_keys(date[0]+"/"+date[1]+"/"+date[2]) submit=browser.find_element_by_xpath("//*[@id='ctl00_MainContent_btnSubmit']") submit.click() time.sleep(500) lexcel,sign=self.readexcel() browser.quit() conn=pymssql.connect(host='.',port='5889',user='******',password='******',database='NgoicDataAutoCollection',charset='utf8') cur=conn.cursor() if sign!="": print sign sql="select Top 1 * from USDA_Exports_Sales where convert(varchar(10),Date,121) = "+"'"+str(sign)+"'" #sql="select Top 1 * from USDA_Exports_Sales" cur.execute(sql) result=cur.fetchall() if len(result)==0: sqlinsert="insert USDA_Exports_Sales values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" cur.executemany(sqlinsert,lexcel) try: conn.commit() except: conn.rollback() cur.close() conn.close()
for filter in [strip_ws, tabs_are_bad]: filter(lines) def strip_ws(lines): for i in xrange(len(lines)): #If the line doesn't consist exclusively of WS if re.match("^[\s]*$", lines[i])==None: lines[i] = lines[i].rstrip() + "\n" def tabs_are_bad(lines): for i in xrange(len(lines)): if (re.match("^\t", lines[i])!=None): lines[i] = re.sub("\t", " ", lines[i]) #--MAIN------------------------------------------------------------------------ nt.chdir(TEST_DIR) for test in TESTS: #Get the contents of the file f = open(test, "r") lines = f.readlines() f.close() #Fix the file apply_filters(lines) #Check out the file try: ec = nt.spawnv(nt.P_WAIT, TF_DIR, ["tf", "edit", test]) except Exception, e: print "FAILED: could not check out %s from TFS: %s" % (test, str(e))
def get_seeds(self): seedfiles = os.listdir(self._seedsdir) for f in seedfiles: if not f.endswith(".done"): continue real_f = f.split(".done")[0] lines = list(file(self._seedsdir+"/"+real_f)) numLine = len(lines) self.logger.debug(u"start file: real_f, with {} lines".format(numLine)) for idxLine, line in enumerate(lines): url, suburl_filter = line.strip().split("\t", 2) self.logger.debug(u"start idxLine: {}/{}".format(idxLine, numLine)) ret = False # conn = getConn("taierdb") # table = 'app_Seeds' try: print "finish parsing txt",datetime.datetime.now() ret = self.get_from_url(url, suburl_filter) print "finish paring page",datetime.datetime.now() query = {'_id':url} # sql = u"""select errorTimes from app_Seeds where url = '{}'""".format(url); # errorTimessql = sqlExecute(sql, conn) errorTimessql = self._mongowrapper.query_one(query,self._seedscolection) errorTimes=0 if errorTimessql: if 'errorTimes'in errorTimessql.keys(): errorTimes = errorTimessql['errorTimes'] # if not errorTimessql[0]['errorTimes']: # errorTimes = 0 # else: # errorTimes = errorTimessql[0]['errorTimes']; if ret: query = {"_id": url.strip(), "domain": suburl_filter} measures = {"fetchTimes": 1,"errorTimes":errorTimes} # sql = u"""update app_Seeds set fetchTimes = 1 where url = '{}'""".format(url); else: # dimensions = {"url": url.strip(), "domain": suburl_filter} errorTimes += 1 query = {"_id": url.strip(), "domain": suburl_filter} measures = {"fetchTimes": 1,"errorTimes":errorTimes} # sql = u"""update app_Seeds set errorTimes = errorTimes + 1, fetchTimes = 1 where url = '{}'""".format(url); except Exception as e: self.logger.warning(str(e)) self.logger.warning(traceback.format_exc()) continue try: self._mongowrapper.find_and_modify(query,measures,self._seedscolection) print "addddddddddddda newlink" #sqlExecute(sql, conn); except Exception as e: self.logger.warning(str(e)) self.logger.warning(traceback.format_exc()) # conn.close() # print "finish db seeds",datetime.datetime.now() name = self._seedsdir+"/"+real_f+'.fetched' chdir(os.path.dirname(self._seedsdir+"/"+real_f)) os.rename(real_f,name) os.remove(real_f+'.done')
def strip_ws(lines): for i in xrange(len(lines)): #If the line doesn't consist exclusively of WS if re.match("^[\s]*$", lines[i]) == None: lines[i] = lines[i].rstrip() + "\n" def tabs_are_bad(lines): for i in xrange(len(lines)): if (re.match("^\t", lines[i]) != None): lines[i] = re.sub("\t", " ", lines[i]) #--MAIN------------------------------------------------------------------------ nt.chdir(TEST_DIR) for test in TESTS: #Get the contents of the file f = open(test, "r") lines = f.readlines() f.close() #Fix the file apply_filters(lines) #Check out the file try: ec = nt.spawnv(nt.P_WAIT, TF_DIR, ["tf", "edit", test]) except Exception, e: print "FAILED: could not check out %s from TFS: %s" % (test, str(e))
# print(soup) for each_li in div.find_all('li'): title = each_li['title'] a_href = each_li.find('a')['href'] voice_id = re.split('\D', a_href)[-1] # print(title, voice_id) info_list[voice_id] = title return info_list if __name__ == '__main__': # from page voice_info_list = get_all_ids(site) # or from file # voice_info_list = {} # with codecs.open('info.txt', 'r', 'utf-8') as fp: # for line in fp: # voice_info_list[re.split('\D', line)[0]] = re.split(' ', line)[1] for file_name in os.listdir(dir_path): try: value = voice_info_list[file_name] # print(file_name, value) if value: pre_name = os.path.join(dir_path, file_name) new_name = os.path.join(dir_path, value.rstrip() + '.mp3') chdir(os.path.dirname(pre_name)) os.rename(pre_name, new_name) print(file_name + ' rename to ' + value.rstrip() + '.mp3') except KeyError as e: print("no such id : " + file_name)