import os
import os.path
#path = 'D:/UC/'
ls = []
def getAppointFile(path,ls):
fileList = os.listdir(path)
try:
for tmp in fileList:
pathTmp = os.path.join(path,tmp)
if True==os.path.isdir(pathTmp):
getAppointFile(pathTmp,ls)
elif pathTmp[pathTmp.rfind('.')+1:].upper()=='PY':
ls.append(pathTmp)
except PermissionError:
pass
def main():
while True:
path = input('请输入路径:').strip()
if os.path.isdir(path) == True:
break
getAppointFile(path,ls)
#print(len(ls))
print(ls)
print(len(ls))
main()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
检索指定路径下后缀是 py 的所有文件
-------------------------------------------------
"""replace strings in text"""
import os
def Replace(file_name, rep_word, new_word):
with open(file_name) as f:
content = []
count = 0
for eachline in f:
if rep_word in eachline:
count += eachline.count(rep_word)
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('文件 {0} 中共有{1}个【{2}】\n您确定要把所有的【{3}】替换为【{4}】吗?\n【YES/NO】:'.format\
(file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
with open(file_name, 'w') as f:
f.writelines(content)
print('Succeed!')
else:
print('Exit!')
if __name__ == '__main__':
while True:
file_name = input('请输入文件名:')
if file_name in os.listdir():
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
Replace(file_name, rep_word, new_word)
break
else:
print('Do not find such a file {}'.format(file_name))
因篇幅问题不能全部显示,请点此查看更多更全内容