说明:这个程序作用是复制一个文件,并且以指定的名字另存这个文件到当前目录。 如果文件不存在,会有错误信息。源文件下载https://static.assets-stash.eet-china.com/album/old-resources/2009/1/23/72228fe8-51d5-4c03-87bb-6064aeb0c6ed.zip #------------------------------------------------------------------------------ # Edit by Long Yang, Python Neophyte import re import sys #1. read the name of input file by following command: # [file] [file to be duplicated to a new file with new name] # change_name.py word.txt #2. A new file named with word_xxx.yyy to be created #3. Copy old file to new file. old_name = sys.argv[1] try: old_file = open(old_name) except IOError: print("No such file or directory: '%s'"%(old_name)) else: old_ext = re.findall('\.\w*',old_name) new_ext = input('Enter the new file format:') new_name = re.sub(old_ext[0], new_ext, old_name) new_file = open(new_name,'w') for line in old_file: new_line = line for words in new_line: new_file.write(words) old_file.close() new_file.close() #-------------------------------------------------------------------------- 运行结果
文章评论(0条评论)
登录后参与讨论