day12 practice

4.1 package a file delete function os.remove ()
as (an example is not accurate, you need to change right):
Write the function principle :
1. The function is designed to try to short
2. function declaration to be reasonable, simple, easy to use
3. function parameters design should be considered backward compatible (version iteration - use)
4. a function of only one thing, try to ensure consistency function statement granularity




import os
"""
需求: 实现文件创建与删除
"""
def filedelect(txt_name):
# 判断是否存在
suffix = ".txt"
f_name = txt_name + suffix
txt_root = os.getcwd()+ '\\test_txt\\'+f_name
try :
os.remove(txt_root)
print("{}删除成功".format(txt_name))
except:
print("{}未找到文件{}".format(txt_root,txt_name))

def new_txt(txt_name):
suffix = ".txt"
f_name = txt_name+suffix
txt_root = os.getcwd() + '\\test_txt\\'
if not os.path.exists(txt_root):
os.makedirs(txt_root)
txt_root += f_name
if not os.path.exists(txt_root):
Open (txt_root, "W")
Print ( "{} created successfully" .format (txt_name))
the else:
Print ( "{} already exists" .format (txt_name))

DEF main ():
the while True:
the try:
Print ( os.listdir (os.getcwd () + '\\ \\ test_txt'))
the except:
Print ( "folder does not exist, create the file")
. Print ( "action item \ n1 create a file deleting a file other 3 enter to quit ")
Work = the iNPUT (" Please enter the action item \ the n-")
IF Work ==" 1 ":
txt_name the iNPUT = (" Please enter the file name to add: \ the n-")
new_txt (txt_name)
elif Work == "2":
FILNAME the iNPUT = ( "Please enter the deleted file name: \ n")
Filedelect (FILNAME)
the else:
BREAK

# execute program entry, put it plainly, run the program from here
if __name__ == "__main__":
main()

 

 

 

 

 






Guess you like

Origin www.cnblogs.com/lyy17759893807/p/12007177.html