# -*- coding:utf-8 -*-
"""
实现一个文件拷贝的功能
"""
import os
import time
# 定义一个获取当前路径下所有文件的函数
def listFile():
workPath = os.getcwd()
return list(os.listdir(workPath))
#定义一个判断文件是否存在的函数
def existsFile(fileName):
if os.path.exists(str(fileName)):
return True
else:
print "File is not exists."
return False
def currentDate():
return time.strftime('%Y-%m-%d',time.localtime(time.time()))
'''
print "Current path:",workPath
countFile = list(os.listdir(workPath))
'''
print "Files list:\n", listFile()
originalName = raw_input("Enter the filename which you want to copy:\n\t")
if existsFile(originalName): #使用函数
#if os.path.exists(str(originalName)): #判断文件是否存在
#读取文件
with open(originalName) as orfile:
context = orfile.readlines()
#新文件生成规则
goalName = originalName[:-4] +str("_") + currentDate() + str(".txt")
#将源文件内容复制到目标文件
with open(goalName, 'a') as toName:
print >> toName, context
print "Copy Done."
print "\t", str(originalName), "has been copied to", str(goalName)
#测试
if __name__ == '__main__':
answerList = ("Y","y","N","n") #定义一个tuple,存储两种选项
if existsFile(originalName):
print "\nWould you want to read the file after copied?"
print "\t'Y':Read \t'N':No"
answer = raw_input()
if answer in answerList[:2]:
with open(goalName) as copiedFile:
print copiedFile.readlines()
elif answer in answerList[2:len(answerList)]:
print "You have choosen 'N',See you!"
else:
print "Other key has been nocked,copied file will not be opened."
else:
pass
Python:当前路径下文件的复制
最新推荐文章于 2024-02-22 00:16:33 发布