0
嗨,我是一個Python初學者,並且不熟悉文件操作。我正在編寫一個用於日誌記錄的python腳本。以下是我的代碼段:使用Python中的時間戳創建文件夾
infile = open('/home/nitish/profiles/Site_info','r')
lines = infile.readlines()
folder_output = '/home/nitish/profiles/output/%s'%datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
folder = open(folder_output,"w")
for index in range(len(lines)):
URL = lines[index]
cmd = "curl -L " +URL
curl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
file_data = curl.stdout.read()
print file_data
filename = '/home/nitish/profiles/output/log-%s.html'%datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
output = open(filename,"w")
output.write(file_data)
output.close()
folder.close()
infile.close()
我不知道這是否正確。我希望每次腳本運行時都創建一個帶有時間戳的新文件夾,並將所有輸出從for循環放入具有時間戳的文件夾中。
感謝您的幫助提前