在创建的文件夹中运行linux命令

2024-04-25 06:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

在linux中,我需要在一个已创建的文件夹中创建一个文件,其思想是每次脚本运行时创建一个文件夹,然后将其放入其他命令的导出中

import datetime
import time
import os

today=time.strftime('%Y%m%d')
hour=time.strftime('%h')
if(hour<12): h = "00"
else: h ="12"
os.system("mkdir /home/xxx/"+str(today)+""+str(h)+"") 

os.system(“touch test.txt /home/xxx/+str(today)+""+str(h)+""”)

Tags: 文件import脚本文件夹hometodaytimeos
1条回答
网友
1楼 · 发布于 2024-04-25 06:23:48
hour=time.strftime('%h')
if(hour<12): h = "00"
else: h ="12"

不起作用hour是一个字符串,可以将其与整数进行比较。除此之外:你根本就不会用h做任何事


全部使用python

import datetime
import time
import os

today=time.strftime('%Y%m%d')
hour=time.strftime('%H')
if(hour == "12"): 
    hour = "00"
path = r"/home/{}/{}{}".format("xxx",today,hour) 

# create path
if not os.path.exists(path ):
    os.makedirs(path)

# create file
with open(path+"/test.txt","w") as f:
    f.write("")

支票:

for root,dirs,files in os.walk("./"):
    print(root,dirs,files)

输出:(下午3点-时区有0-24小时时钟)

#root , dirs, files
./ ['xxx'] ['main.py']
./xxx ['2019060315'] []
./xxx/2019060315 [] ['test.txt']

相关问题 更多 >