3

我必须通过几个请求从网上下载文件。每个请求的下载文件必须放在与请求编号同名的文件夹中。

例如:

我的脚本现在正在运行以下载请求号 87665 的文件。所以所有下载的文件都将放在目标文件夹Current Download\Attachment87665中。那么我该怎么做呢?

到目前为止我已经尝试过:

my_dir = "D:\Current Download"
my_dir = os.path.expanduser(my_dir)
if not os.path.exists(my_dir):
    os.makedirs(my_dir)

但它不符合我原来的要求。知道如何实现这一目标吗?

4

1 回答 1

10

只需事先创建一条路径,通过os.path.join

request_number = 82673

# base dir
_dir = "D:\Current Download"       

# create dynamic name, like "D:\Current Download\Attachment82673"
_dir = os.path.join(_dir, 'Attachment%s' % request_number)

# create 'dynamic' dir, if it does not exist
if not os.path.exists(_dir):
    os.makedirs(_dir)
于 2013-01-08T09:13:53.283 回答