Python。代码没有接收到list中的所有字符串值。

问题描述 投票:0回答:2

我创建了以下代码来创建一些文件夹。

import os

Country = {"Albania - AL",
"Armenia - AM",
"Austria - AT",
"Bahrain - BH",
"Belarus - BY",
"Belgium - BE",
"Bulgaria - BG",
"Canary Islands - KY",
"Croatia - HR",
"Cyprus - CY",
"Czech Republic - CZ",
"Denmark - DK",
"Estonia - EE",
"Finland - FI",
"France - FR",
"Germany - DE",
"Greece - GR",
"Hungary - HU",
"Iceland - IS",
"Ireland - IE",
"Israel - IL",
"Italy - IT",
"Latvia - LV",
"Lithuania - LT",
"Luxembourg - LU",
"Malta - MT",
"Morocco - MA",
"Netherlands - NL",
"Norway - NO",
"Poland - PL",
"Portugal - PT",
"Romania - RO",
"Russian Federation - RU",
"Saudi Arabia - SA",
"Serbia - RS",
"Slovakia - SK",
"Slovenia - SI",
"South Africa - ZA",
"Spain - ES",
"Sweden - SW",
"Switzerland - CH",
"Turkey - TR",
"United Arab Emirates - AE",
"United Kingdom Of Great Britain And Northern Ireland - UK",
}

for x in Country:
    path = ("C:/Users/thrnma/Documents/VAT Reporting/" + x)

try:
    os.mkdir(path)
except OSError:
    print ("Creation of the directory %s failed" % path)
else:
    print ("Successfully created the directory %s " % path)

当我运行该程序时,它只生成一个沙特阿拉伯的文件夹。我试着运行了多次,但它不会生成任何新的文件夹。我想我需要做一些事情,比如循环运行,但我不知道该怎么做 (我对 Python 还是个新手)。

python string list directory subdirectory
2个回答
2
投票

你没有把 "创建目录 "的部分放在你的循环中。这应该可以,试着理解其中的区别。

import os

Country = {"Albania - AL",
"Armenia - AM",
"Austria - AT",
"Bahrain - BH",
"Belarus - BY",
"Belgium - BE",
"Bulgaria - BG",
"Canary Islands - KY",
"Croatia - HR",
"Cyprus - CY",
"Czech Republic - CZ",
"Denmark - DK",
"Estonia - EE",
"Finland - FI",
"France - FR",
"Germany - DE",
"Greece - GR",
"Hungary - HU",
"Iceland - IS",
"Ireland - IE",
"Israel - IL",
"Italy - IT",
"Latvia - LV",
"Lithuania - LT",
"Luxembourg - LU",
"Malta - MT",
"Morocco - MA",
"Netherlands - NL",
"Norway - NO",
"Poland - PL",
"Portugal - PT",
"Romania - RO",
"Russian Federation - RU",
"Saudi Arabia - SA",
"Serbia - RS",
"Slovakia - SK",
"Slovenia - SI",
"South Africa - ZA",
"Spain - ES",
"Sweden - SW",
"Switzerland - CH",
"Turkey - TR",
"United Arab Emirates - AE",
"United Kingdom Of Great Britain And Northern Ireland - UK",
}

for x in Country:
    path = ("C:/Users/thrnma/Documents/VAT Reporting/" + x)

    try:
        os.mkdir(path)
    except OSError:
        print ("Creation of the directory %s failed" % path)
    else:
        print ("Successfully created the directory %s " % path)

2
投票

只要把你的try block的缩进部分修改一下就可以了. 就是这样。

它应该是这样的。

for x in Country:
    path = ("C:/Users/thrnma/Documents/VAT Reporting/" + x)

    try:
        os.mkdir(path)
    except OSError:
        print ("Creation of the directory %s failed" % path)
    else:
        print ("Successfully created the directory %s " % path)

0
投票

os.mkdir(path) 部分内为环。

for x in Country:
    path = ("C:/Users/thrnma/Documents/VAT Reporting/" + x)

    try:
        os.mkdir(path)
    except OSError:
        print ("Creation of the directory %s failed" % path)
    else:
        print ("Successfully created the directory %s " % path)
© www.soinside.com 2019 - 2024. All rights reserved.