简体   繁体   English

如何复制时间戳文件夹中的文件

[英]How to copy a file in the time stamp folder

I make a directory with the current timestamp 12:32:57 PM.我用当前时间戳 12:32:57 PM 创建了一个目录。 I want to copy the file to the time folder who can I do this?我想将文件复制到时间文件夹,我可以这样做吗? I run the following code我运行以下代码

import os  
import shutil 

cmd_mkdir = 'mkdir /home/mubashir/catkin_ws/src/germany1_trush/ros_bag_saved/"$(date +%r)"'
os.system(cmd_mkdir)

origin_past=f'/home/mubashir/catkin_ws/src/germany1_trush/rosbag/1.bag'
target_past=f'/home/mubashir/catkin_ws/src/germany1_trush/ros_bag_saved/$(date +%r)/1.bag'
shutil.copy(origin_past,target_past)

What you want to do is grab the time in python and save it in a variable, that way you can reference the same time for both creating the folder and copying the file into it.你想要做的是抓取 python 中的时间并将其保存在一个变量中,这样你就可以在创建文件夹和将文件复制到其中时参考相同的时间。

You could use你可以使用

import os
import shuitil
from datetime import datetime
time = datetime.now().strftime('%I:%M:%S %p')

This will get the time in the format you have above (ie 12:32:57 PM)这将以您上面的格式获取时间(即 12:32:57 PM)

Then update your commands to use it:然后更新您的命令以使用它:

cmd_mkdir = f'mkdir /home/mubashir/catkin_ws/src/germany1_trush/ros_bag_saved/{time}'
target_past = f'/home/mubashir/catkin_ws/src/germany1_trush/ros_bag_saved/{time}/1.bag'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM