Storbinary格式字符串Python3 FTPLib

2024-05-15 02:39:33 发布

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

marlin=open(bacteria,'rb')
session.storbinary('STOR bacteria.png',open(bacteria,'rb'))

所以基本上我想改名细菌.png每次我运行脚本。我想在脚本运行时(“10:00下午.png"). 你知道吗

bacteria=time.time()
bacteria=str(bacteria)+".png"

如何在storbinary中使用格式字符串。你知道吗


Tags: 字符串脚本timepngsession格式open细菌
1条回答
网友
1楼 · 发布于 2024-05-15 02:39:33

你可以这样做:

import time

# Get the current time and format as a string
timestring = time.strftime('%H:%M%p', time.gmtime()).lower()
# Create a format string for the STOR command
stor_format = 'STOR {}.png'
# Format the format string with the timestring
session.storbinary(stor_format.format(timestring), open(bacteria,'rb'))

^{}获取UTC时间-如果您喜欢使用本地时间,请使用^{}。你知道吗

^{}使用提供的格式将时间格式化为字符串。格式字符串'%H:%S%p'将时间格式化为两位数的小时和分钟,用冒号分隔,后跟'AM'或'PM'。对.lower()的结果调用time.strftime()会将“AM”或“PM”转换为小写形式:“AM”或“PM”。你知道吗

通过用'{}'占位符替换STOR命令中的单词“bacteria”,可以创建所需的STOR命令。你知道吗

>>> time_string = time.strftime('%H:%M%p', time.gmtime()).lower()
>>> print(time_string)
12:54pm
>> stor_format = 'STOR {}.png'
>>> stor_format.format(time_string)
'STOR 12:54pm.png'

相关问题 更多 >

    热门问题