"""笨办法学python,启动时在命令行中python 脚本文件名 filename"""from sys import argv # argv - 命令行参数; argv[0]是脚本路径名python,sys.argv[0]表示脚本路径名script, filename = argv # 脚本和文件名称print("We‘re going to erase %r." % filename) # 正常使用%s,在这里%r是为了重现它代表的对象print("If you don‘t want that, hit RETURN-C (^C).")print("If you do want that, hit RETURN.")input("?") # 单纯的?号print("Opening the file...")target = open(filename, "w") # 以"w"写入的形式打开文件,a表示追加,r表示读取。不写默认是读取操作print("Truncating the file. Goodbye!")target.truncate() # 清除之前的数据print("Now I‘m going to ask you for three lines.")line1 = input("line 1: ") # 获取用户输入line2 = input("line 2: ")line3 = input("line 3: ")print("I‘m going to write these to the file.")target.write(line1) # 写入用户输入target.write("\n")target.write(line2)target.write("\n")target.write(line3)target.write("\n")print("And finally, we close it.")target.close() # 关闭已打开的文件
原文地址:https://www.cnblogs.com/Guishuzhe/p/10206384.html
时间: 2024-11-05 11:53:52