我正在使用以下功能每周上传文件和结构,
我希望在此处调整以将目录结构更改为每年/每月/每天
example / Incoming / 2016/02/08 / filename
如果dir或sub dir没有退出,它将创建它然后上传
应该使用哪种方法从每周一天切换到每年/每月/每天的深层目录结构
def dir_today ():
""" Function to determine directory today """
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day = week[datetime.today().weekday()]
return day
def ftp_upload (ifn):
"""" FTP upload Function. Destination file will be appended with date and timecode received from meta data"""
src = "%s/%s" % (src_loc,ifn)
try:
create_time = get_meta(ifn)
except:
create_time = datetime.now().strftime("VID_%Y-%m-%d_T_%H-%M-%S")
dst_file = os.path.splitext(ifn)[0] + os.path.splitext(ifn)[1]
upload_directory = ftp_base_directory + '/' + dir_today() + '/'
try:
ftp_connect = ftplib.FTP(ftp_server, ftp_login, ftp_password)
try:
ftp_connect.cwd(upload_directory)
except ftplib.all_errors:
ftp_connect.mkd(upload_directory)
ftp_connect.cwd(upload_directory)
file = open(src, "rb")
send = ftp_connect.storbinary("STOR "+ dst_file, file)
ftp_connect.close
file.close()
os.remove(src)
logger.info("Moved file %s" %(src))
oflist.remove(ifn)
hsize.pop(ifn)
uploaded.insert(0,ifn)
try:
uploaded.pop(9)
except:
pass
except Exception as e:
logger.info("Error: %s" %(repr(e)))
return
答案 0 :(得分:1)
您可以修改dir_today()
以返回子路径:
def dir_today():
today = date.today()
return '{day.year}/{day.month}/{day.day}'.format(day=today)
答案 1 :(得分:1)
或者:
>>> def dir_today():
... return datetime.datetime.today().strftime("%Y/%m/%d")
...
>>> dir_today()
'2016/02/08'