Python:代码未选取列表中的所有字符串值
Python: Code is not picking up all string values in list
我创建了以下代码来创建多个文件夹
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)
当我运行程序时,它只为沙特阿拉伯生成一个文件夹。我多次尝试 运行ning 它,但它不会生成任何新文件夹。我假设我需要做一些类似循环的事情,但我不确定该怎么做(在 Python 仍然是新手)。
提前谢谢你。
您没有将 "creating directory" 部分放入循环中。
这应该有效,尝试了解其中的区别:
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)
只需修复 try 块的缩进。就是这样。
应该是这样的:
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)
将 os.mkdir(path)
部分放在 for 循环中。
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)
我创建了以下代码来创建多个文件夹
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)
当我运行程序时,它只为沙特阿拉伯生成一个文件夹。我多次尝试 运行ning 它,但它不会生成任何新文件夹。我假设我需要做一些类似循环的事情,但我不确定该怎么做(在 Python 仍然是新手)。 提前谢谢你。
您没有将 "creating directory" 部分放入循环中。
这应该有效,尝试了解其中的区别:
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)
只需修复 try 块的缩进。就是这样。
应该是这样的:
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)
将 os.mkdir(path)
部分放在 for 循环中。
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)