f = open('E:/test.txt', 'w') # A f = open('E:\\test.txt', 'w') # C f = open('E:\\Test.bin', 'xb') #注意的是'x'和'w'均是以“可写入”的模式打开文件,但以'x'模式打开的时候,如果路径下已经存在相同的文件名,会抛出异常,而'w'模式的话会直接覆盖同名文件(比较危险)。
编写一个函数 findstr(),该函数统计一个长度为 2 的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为“You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.”,子字符串为“im”,函数执行后打印“子字母串在目标字符串中共出现 3 次”。
#方法1:count()方法 deffindstr1(): string = "You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted." temp = input("请输入字符串:") if temp.isspace() or len(temp) <= 2: temp = string temp1 = input("输入查找的字符串(两个以上):")
while len(temp1) < 2: temp1 = input("查找字符长度有误,请重新输入:")
print(temp.count(temp1))
findstr1()
#方法2: deffindStr(desStr, subStr): count = 0 length = len(desStr) if subStr notin desStr: print('在目标字符串中未找到字符串!') else: for each1 in range(length-1): if desStr[each1] == subStr[0]: #主要代码在这里判断连续的两个字符是不相等 if desStr[each1+1] == subStr[1]: count += 1 print('子字符串在目标字符串中共出现 %d 次' % count)
defpalindrome(string): length = len(string) last = length-1 length //= 2 flag = 1 for each in range(length): if string[each] != string[last]: flag = 0 last -= 1
if flag == 1: return1 else: return0
string = input('请输入一句话:') if palindrome(string) == 1: print('是回文联!') else: print('不是回文联!')
str1 = '''拷贝过来的字符串''' list1 = [] for each in str1: if each notin list1: #如果不在 list1 中,执行统计 if each == '\n': print('\\n', str1.count(each)) else: print(each, str1.count(each)) #使用比较精辟 list1.append(each)
while1: instr = int(input('\n请输入相关的指令代码:')) if instr == 1: name = input('请输入联系人姓名:') if name in contacts: print(name + ' : ' + contacts[name]) else: print('您输入的姓名不再通讯录中!')
if instr == 2: name = input('请输入联系人姓名:') if name in contacts: print('您输入的姓名在通讯录中已存在 -->> ', end='') print(name + ' : ' + contacts[name]) if input('是否修改用户资料(YES/NO):') == 'YES': contacts[name] = input('请输入用户联系电话:') else: contacts[name] = input('请输入用户联系电话:')
if instr == 3: name = input('请输入联系人姓名:') if name in contacts: del(contacts[name]) # 也可以使用dict.pop() else: print('您输入的联系人不存在。') if instr == 4: break
deffile_view(file_name, line_num): if line_num.strip() == ':': begin = '1' end = '-1' (begin, end) = line_num.split(':')
if begin == '': begin = '1' if end == '': end = '-1'
if begin == '1'and end == '-1': prompt = '的全文' elif begin == '1': prompt = '从开始到%s' % end elif end == '-1': prompt = '从%s到结束' % begin else: prompt = '从第%s行到第%s行' % (begin, end)
print('\n文件%s%s的内容如下:\n' % (file_name, prompt))
begin = int(begin) - 1 end = int(end) lines = end - begin
f = open(file_name) for i in range(begin): # 用于消耗掉begin之前的内容 f.readline()
if lines < 0: print(f.read()) else: for j in range(lines): print(f.readline(), end='') f.close()
for i in all_files: for each_file in i[2]: if os.path.splitext(each_file)[1] == '.txt': # 根据后缀判断是否文本文件 each_file = os.path.join(i[0], each_file) txt_files.append(each_file)
for each_txt_file in txt_files: key_dict = search_in_file(each_txt_file, key) if key_dict: print('================================================================') print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key)) if detail in ['YES', 'Yes', 'yes']: print_pos(key_dict)
#!/usr/bin/python3 #功能:代码行数统计 import os count = 0 # 代码行数
def filecount(dirpath): global count os.chdir(dirpath) for each_file in os.listdir(os.curdir): if os.path.isfile(each_file): with open(each_file,'r',encoding='utf8') as f: for index,value in enumerate(f): count += 1 elif os.path.isdir(each_file): filecount(each_file) os.chdir(os.pardir)
方式1.请访问本博主的B站【WeiyiGeek】首页关注UP主, 将自动随机获取解锁验证码。
Method 2.Please visit 【My Twitter】. There is an article verification code in the homepage.
方式3.扫一扫下方二维码,关注本站官方公众号
回复:验证码
将获取解锁(有效期7天)本站所有技术文章哟!