python first training

创建,删除,重命名文件在这里插入代码片

创建

#!/usr/bin/pyhon
#--coding :utf-8--
file=open(‘D:/py/1.txt’,‘w’)
file.write(‘hello,\n tomorrow’)

删除

import os
path=‘D:/1.txt’
if os.path.exists(path):
os.remove(path)
else:
print(‘no such file’)

重命名

import os
Srcfile=‘D:/py/1.txt’
Dstfile=‘D:/py/hello.txt’
try:
os.rename(Srcfile,Dstfile)
except Exception as e:
print(e)
print(‘rename file file\n’)
else:
print(‘rename file sucess\n’)

对列表进行排序

import os
list=[]
n=input(“input element quantity\n”)
n=int(n)
for i in range (0,n,1):
temp=input(“input element\n”)
list.append(temp)
list.sort()
print(list)

对列表进行去重

python first training
三种方法
list =set(list) 利用set函数
dict=dict.fromkeys(列表名,键值)
利用index函数,index返回第一次出现的位置
if list.index(list[cursor])==cursor :
list2.append(list[cursor])

字典学号姓名

python first training