使用日期时间名称和目录中的子文件创建目录 (Python)

发布于 2024-12-12 07:44:28 字数 1040 浏览 4 评论 0原文

我目前正在寻找使用 Python v2.7 在 Linux 上创建一个目录,目录名称为日期和时间(即 27-10-2011 23:00:01)。我的代码如下: -

import time
import os

dirfmt = "/root/%4d-%02d-%02d %02d:%02d:%02d"
dirname = dirfmt % time.localtime()[0:6]
os.mkdir(dirname)

该代码工作正常并根据要求生成目录。尽管如此,我还想在这个目录中创建两个同名的 csv 文件和一个日志文件。现在,由于目录名称是动态生成的,我不确定如何移入该目录来创建这些文件。我希望目录和三个文件都具有相同的名称(csv 文件将以字母为前缀)。例如,鉴于上述情况,我想要创建一个名为“27-10-2011 23:00:01”的目录,然后在其中创建两个名为“a27-10-2011 23:00:01.csv”的csv文件”和“b27-10-2011 23:00:01.csv”以及名为“27-10-2011”的日志文件23:00:01.log”。

我的文件创建代码如下:-

csvafmt = "a%4d-%02d-%02d %02d:%02d:%02d.csv"
csvbfmt = "b%4d-%02d-%02d %02d:%02d:%02d.csv"
logfmt = "%4d-%02d-%02d %02d:%02d:%02d.log"

csvafile = csvafmt % time.localtime()[0:6]
csvbfile = csvbfmt % time.localtime()[0:6]
logfile = logfmt % time.localtime()[0:6]

fcsva = open(csvafile, 'wb')
fcsvb = open(csvbfile, 'wb')
flog = open(logfile, 'wb')

有什么建议我可以如何做到这一点,以便第二个始终保持不变?我很欣赏这段代码只需要一瞬间的时间来运行,但在那段时间内,第二个可能会改变。我认为关键在于改变“time.localtime”,但我仍然不确定。

谢谢

I'm currently looking to create a directory on Linux using Python v2.7 with the directory name as the date and time (ie. 27-10-2011 23:00:01). My code for this is below:-

import time
import os

dirfmt = "/root/%4d-%02d-%02d %02d:%02d:%02d"
dirname = dirfmt % time.localtime()[0:6]
os.mkdir(dirname)

This code works fine and generates the directory as requested. Nonetheless, what I'd also like to then is, within this directory create two csv files and a log file with the same name. Now as the directory name is dynamically generated, I'm unsure as to how to move into this directory to create these files. I'd like the directory together with the three files to all have the same name (csv files will be prefixed with a letter). So for example, given the above, I'd like a directory created called "27-10-2011 23:00:01" and then within this, two csv files called "a27-10-2011 23:00:01.csv" and "b27-10-2011 23:00:01.csv" and a log file called "27-10-2011 23:00:01.log".

My code for the file creations is as below:-

csvafmt = "a%4d-%02d-%02d %02d:%02d:%02d.csv"
csvbfmt = "b%4d-%02d-%02d %02d:%02d:%02d.csv"
logfmt = "%4d-%02d-%02d %02d:%02d:%02d.log"

csvafile = csvafmt % time.localtime()[0:6]
csvbfile = csvbfmt % time.localtime()[0:6]
logfile = logfmt % time.localtime()[0:6]

fcsva = open(csvafile, 'wb')
fcsvb = open(csvbfile, 'wb')
flog = open(logfile, 'wb')

Any suggestions how I can do this so that the second remains the same throughout? I appreciate this code would only take a split second to run but within that time, the second may change. I assume the key to this lies within altering "time.localtime" but I remain unsure.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

躲猫猫 2024-12-19 07:44:28

当然,只需将时间保存在变量中,然后使用该变量进行替换:

now = time.localtime()[0:6]
dirname = dirfmt % now
csvafile = os.path.join(dirname, csvafmt % now)
csvbfile = os.path.join(dirname, csvbfmt % now)
logfile = os.path.join(dirname, logfmt % now)

编辑以包括创建 csv 和日志文件的完整路径。

Sure, just save the time in a variable and then use that variable for the substitutions:

now = time.localtime()[0:6]
dirname = dirfmt % now
csvafile = os.path.join(dirname, csvafmt % now)
csvbfile = os.path.join(dirname, csvbfmt % now)
logfile = os.path.join(dirname, logfmt % now)

Edited to include creating the complete path to your csv and log files.

傾旎 2024-12-19 07:44:28

仅调用 time.localtime 一次。

current_time = time.localtime()[0:6]

csvafile = csvafmt % current_time 
csvbfile = csvbfmt % current_time 
logfile = logfmt % current_time

Only call time.localtime once.

current_time = time.localtime()[0:6]

csvafile = csvafmt % current_time 
csvbfile = csvbfmt % current_time 
logfile = logfmt % current_time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文