[英]Sequential Directory Names Python 3
Im trying to generate directories that are numbered 1 to however many. 我正在尝试生成编号为1到许多的目录。 because in python you have to convert an integer to string, which changes it to lexicographical, how do I still keep the order of the directories?
因为在python中,您必须将整数转换为字符串,从而将其更改为字典,我如何仍保持目录的顺序?
I want the directories when generated to look like this 我希望生成的目录看起来像这样
dir1
dir2
dir3
dir4
dir5
dir6
...
Instead I get the output of 相反,我得到的输出
dir0
dir1
dir10
dir11
dir12
dir13
dir14
...
dir19
dir2
dir20
dir21
dir22
dir23
...
my code 我的密码
totaldir = 20
while (x < totaldir):
x += 1
print ("start!!!")
makestr = str("dir%s" % x)
makedir = os.makedirs(makestr, mode=0o755, exist_ok=True)
print (makedir)
print ("ended!!!")
By output you mean you are seeing them that way in the directory from a File viewer software ( explorer in windows ). 通过输出,您的意思是您正在通过文件查看器软件(Windows中的资源管理器)在目录中以这种方式看到它们。
That is because the fileviewer / explorer has been configured ( by default ) to display the contents in alphabetically sorted manner. 这是因为fileviewer / explorer已配置(默认情况下)以按字母排序的方式显示内容。 By alphabetically sorted manner
10
comes before 2
, hence the order of dir0
then dir1
then dir10
. 通过按字母顺序排序的方式
10
来之前 2
,的因此顺序dir0
然后dir1
然后dir10
。
If you want them to be displayed the way you expect, follow @jonrsharpe's suggestion of 0 padding the file number, so that they remain alphabetically in the same order as their numerical order. 如果希望以期望的方式显示它们,请遵循@jonrsharpe的建议,即对文件号进行0填充,以便它们按字母顺序保持与数字顺序相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.