我需要将一个文件复制到一个子目录,其名称更改为name_timestamp
(通过附加时间戳)。你知道吗
我正在csv文件上使用一个副本method
,一旦复制完成,我需要将csv文件移动到一个子目录并将其重命名为CSV_timestamp
。你知道吗
下面是示例代码。有人能帮我或建议我怎么做吗?你知道吗
import os, shutil, time
if not os.path.exists(dirName):
os.mkdir(dirName)
print("Directory " , dirName , " Created ")
else:
print("Directory " , dirName , " already exists")
def copyFile(src, dest):
try:
shutil.copy(src, dest)
# eg. src and dest are the same file
except shutil.Error as e:
print('Error: %s' % e)
# eg. source or destination doesn't exist
except IOError as e:
print('Error: %s' % e.strerror)
os.rename("path/to/current/age.csv", "path/to/new/destination/for/age.csv")
shutil.move("path/to/current/age.csv", "path/to/new/destination/for/age.csv")
你可以用操作系统重命名()仪表
它应该很简单,只要在完成时将目标路径设置为您想要的任何目录。你知道吗
例如,假设您的文件位于users/foo/bar中/我的文件.csv(这将是您的
src
路径)。假设您希望该文件的副本位于users/mydocuments/bar/foo中/我的新文件.csv(这将是您的dest
路径)。你知道吗你需要做的就是:
按照您的方法(使用检查目录是否存在的函数等),您可以执行以下操作:
请注意
YYYY_MM_DD
格式的戳记 但是你可以很容易地改变,看到了吗 documentation在time
包上。你知道吗shutil.copy
之后,您不再需要shutil.move
,因为 第一个已复制了您的文件并将其保存到目标 路径。你知道吗相关问题 更多 >
编程相关推荐