gpt4 book ai didi

python - 在 FTP 中使用 Python 创建年/月/日文件夹结构

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:15:20 26 4
gpt4 key购买 nike

我正在寻找使用 Python ftplib 模块创建年/月/日文件夹结构的选项

连接:

ftp = ftplib.FTP(ftp_servidor, ftp_usuario, ftp_clave)

加载变量:

ftp_raiz = 'TEST/'
filename = '2019-10-01T00-00-00.txt'

功能:

def cdTree(ftp, filename=None, path=None):

if filename is not None:
date = datetime.datetime.strptime(filename, '%Y-%m-%dT%H-%M-%S.txt')
path = ftp_raiz + date.strftime('%Y') + '/' + date.strftime('%m') + '/' + date.strftime('%d')
print filename
if path != "":
try:
ftp.cwd(path)
except error_perm as e:
print e, ", creating folder"
print path
cdTree(ftp, path="/".join(path.split("/")[:-1]))
ftp.mkd(path)
ftp.cwd(path)

cdTree(ftp, filename, 'TEST')

最后我没有创建文件夹结构,它抛出以下错误:

2018-10-18T00-00-00.txt
550 Failed to change directory. , creating folder
2018/10/18
550 Failed to change directory. , creating folder
2018/10
Traceback (most recent call last):
File "ftp2.py", line 34, in <module>
cdTree(ftp, filename)
File "ftp2.py", line 30, in cdTree
cdTree(ftp, path="/".join(path.split("/")[:-1]) )
File "ftp2.py", line 31, in cdTree
ftp.mkd(path)
File "C:\python27\lib\ftplib.py", line 589, in mkd
resp = self.sendcmd('MKD ' + dirname)
File "C:\python27\lib\ftplib.py", line 251, in sendcmd
return self.getresp()
File "C:\python27\lib\ftplib.py", line 226, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Create directory operation failed.

注意:进行只创建一个文件夹的测试,成功!

最佳答案

你的代码对我来说没有多大意义。

我相信你想要这个:

def cdTree(ftp, path):
print "entering folder {0}".format(path)
try:
ftp.cwd(path)
except:
print "failed to enter, creating"
cdTree(ftp, path="/".join(path.split("/")[:-1]))
ftp.mkd(path)
ftp.cwd(path)

ftp_raiz = 'TEST/'
filename = '2019-10-01T00-00-00.txt'

date = datetime.datetime.strptime(filename, '%Y-%m-%dT%H-%M-%S.txt')
path = ftp_raiz + date.strftime('%Y/%m/%d')

cdTree(ftp, path)

关于python - 在 FTP 中使用 Python 创建年/月/日文件夹结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52989360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com