【发布时间】:2014-09-22 03:01:28
【问题描述】:
我写了一个函数来在 python 中创建一个目录。如果已使用原始名称,则该函数将扩展名(例如 _1)添加到目录名称。 该功能按预期工作并创建文件夹;但是返回值有问题。当我打印出来时,我没有。这是我的代码。可能不是最干净的:s。这与在函数内部调用自身有关,但我不确定如何解决。
import os
##function to generate a dir with extension if it already exists
def createDir(directory,ext):
thePath=directory+ext
if not os.path.exists(thePath):
os.makedirs(thePath)
output=thePath + '/'
print 'I return ' + output #I got "I return /media/usb0/incomplete_noNameYet_2/ (Because incomplete_noNameYet and incomplete_noNameYet_1 already existed)" This is fine!
return output
else:
if ext =='':
ext='_1'
else:
ext= '_' + str(int(ext[1:len(ext)])+1)
createDir(directory,ext)
def main():
print createDir('/media/usb0/incomplete_noNameYet','') #I got none.
if __name__ == '__main__':
main()
【问题讨论】:
标签: python recursion directory