37,720
社区成员
发帖
与我相关
我的任务
分享
for root,dirnames,filenames in os.walk(TestCasesDir):
for myFile in filenames:
TxtFile = os.path.join(root,myFile)
if "txt" in myFile:
在 if "txt" in myFile: 补上搜索所有文件的遍历循环就行了[/quote]
非常感谢,解决了好多问题,再次谢谢
for root,dirnames,filenames in os.walk(TestCasesDir):
for myFile in filenames:
TxtFile = os.path.join(root,myFile)
if "txt" in myFile:
在 if "txt" in myFile: 补上搜索所有文件的遍历循环就行了import os
import re
import shutil
import time
def MainFunction():
a = raw_input ("""
1. Replace Content on Folder
2. Copy Models
Press Any Key to Exit
Please choose : """)
if (a == "1" or a == "2"):
TestCasesDir = raw_input ("Please Input your folder Path: ")
if a == "1":
s = raw_input("Please Input what words you want to find: ")
p = raw_input("Please Input what words you want to replace: ")
#TestCasesDir = "C:\\Users\\t_zhanj\\Desktop\\Cases"
if os.path.exists(TestCasesDir):
ReplaceContentOnDir(TestCasesDir,s,p)
else:
print "This folder not exist!"
if a == "2":
s = 'RP'
CopyMyModels(TestCasesDir,s)
else:
exit(0)
def ReplaceContentOnDir(TestCasesDir,s,p):
Dir = os.listdir(TestCasesDir)
for myFile in Dir:
TxtFile = os.path.join(TestCasesDir,myFile)
if "txt" in myFile:
#print "File Path is: " + TxtFile #Show the txt file path(Should open this)
#print "File Name is: " + myFile #Show the txt file name
print "================================ Start ================================"
print myFile+ " is reading....."
files = open (TxtFile,'r') #read mode
content = files.read()
files.close()
if s in content:
print "Found word: " + s + " in File " + myFile
files = open (TxtFile,'w') # write mode
files.writelines(content.replace (s, p))
files.close()
print s + " has been replaced as " + p + " Success!"
#f = open (TxtFile,'r')
#content = f.read()
#f.close()
else:
print s + " Not found on File!"
#f.close()
print "================================ Finish ==============================="
CountineOrExit()
def CopyMyModels(TestCasesDir,s):
Dir = os.listdir(TestCasesDir)
for myFile in Dir:
TxtFile = os.path.join(TestCasesDir,myFile)
if "txt" in myFile:
i=0
print "=============Start on Copy Sample Files================="
print myFile+ " is reading"
files = open (TxtFile,'r') #read mode
AllLines = files.readlines()
#print AllLines,type(AllLines)
# Use Readlines to read all content on txt file and for cycle to convert to str type
for eachline in AllLines:
#replace " as space in order to clear some noise
eachline = eachline.replace('"','')
#print eachline
match = re.search(r'^([a-zA-Z]:|//[a-zA-Z0-9_.$ -]+/[a-z0-9_.$ -]+)?((?:/|^)(?:[^//:*?"<>|\r\n]+/)+)', eachline)
extname = re.findall(r'\[^.\\/:*?"<>|\r\n]+$', eachline)
filename = re.findall(r'[^\\/:*?"<>|\r\n]+$' , eachline)
if s in eachline:
if match:
filepath = match.group(2)
else:
filepath = ""
#Use method find to find the index of what you want to search.Here I found word:RP
search = 'RP'
start = 0
index = eachline.find(search, start)
#Should add 3 to get the real path of model,RP in 27,should get it start after "RP=" that add 3 index
index +=3
#print index
#Use split to get the model from which type
#print "file path is " + filepath[index:]
SubFolder = filepath[index:].split('/')[-2]
#print "file name is " + filename[0]
#print "SubFolder is " +SubFolder
myFusionPath = r'C:\\Users\\t_zhanj\\AppData\\Local\\Autodesk\\webdeploy\\dev\\70f3c4c6ffa63f9c51e83d924d85542ac308a9bc'
ModelPath = os.path.join (myFusionPath,filepath[index:],filename[0])
#print "ModelPath is " + ModelPath
if os.path.exists(ModelPath):
TargetFolder = r'C:\\Users\\t_zhanj\\Desktop\\Cases\\ModelTestingFolder'
TargetFolder = os.path.join(TargetFolder,SubFolder)
if os.path.exists(TargetFolder):
print TargetFolder + " existed!"
else:
#Makedirs can create multi level folder
#mkdir only crate one level folder
os.makedirs(TargetFolder)
#print TargetFolder + " create success!"
# Copy Files use shutil copy method
# shutil (sourceFolder,TargetFolder)
CopiedModelPath = os.path.join(TargetFolder,filename[0])
print CopiedModelPath
if os.path.exists(CopiedModelPath):
#get the name without [''] on list,so use filename[0]
print filename[0] + " has been copied,do not need copy again!"
else:
shutil.copy (ModelPath,TargetFolder)
#print "CopiedModelPath is " + CopiedModelPath
#get the name without [''] on list,so use filename[0]
print "Copy " + filename[0]+ " Success! "
else:
print "Model path " + ModelPath + " not existed!"
"""
if i == 0:
print s + " didn't found on " + myFile
elif i == 1:
print s + " has been found for " + str(i) + " time on " + myFile
else:
print s + " have been found for " + str(i) + " times on " + myFile
"""
files.close()
print "=============Finish on Copy Sample Files================="
CountineOrExit()
def CountineOrExit():
IsExit = raw_input ("Countine(Y) or Exit(N)? ")
while(1):
if IsExit.upper() == "Y":
MainFunction()
elif IsExit.upper() == "N":
exit(0)
else:
print "You have inputed the illegal character,try again!"
CountineOrExit()
break
if __name__=='__main__':
MainFunction()
楼主看看吧,基本上应该能实现这些功能了,有问题可以再问
import os,sys
whichfile = 'test' #所有名称中包含test的文件
whichext = '.txt' #所有txt文件
startdir = '.'
findstr = 'ABC'
replacestr = 'abc'
searchstr = 'abc.f3d'
for root, dirs, files in os.walk(startdir, topdown=False):
for name in files:
if whichext in name and whichfile in name:
thisname = os.path.join(root, name)
print('\nfile:[',thisname,']\npress enter to read this file...')
input()
with open(thisname, 'r') as f:
for line in f.readlines():
#1 newline = line.replace(findstr, replacestr)
#2 myindex = line.find(searchstr) ...return index
print(line,end='')