我想将文件夹名称存储为变量x,以便以后可以在其他函数中使用它,但是当我使用x时,它告诉我它是未定义的。但是只有一次该函数再次执行。
import os
def start():
s = 0
for folder in next(os.walk('E:'))[1][s]:
x = folder #for each value of s, make x = the folder's name
s += 1
return x
if __name__ == '__start__':
start()
fdrname = r'E:\\' + x
stem = fdrname[4:6]
n = stem + 'F'
def main():
fdrname = r'E:\\' + x
stem = fdrname[4:6]
n = stem + 'F'
i = 0
for filename in os.listdir(fdrname):
dst = x + n + str(i) + ".jpg"
src = x + filename
os.rename(src, dst)
i += 1
if __name__ == '__main__':
main(x)
print(fdrname)
当我运行它时,这就是输出
----> 21 main(x)
NameError: name 'x' is not defined
答案 0 :(得分:0)
只需从函数返回x并将其存储在main()中到其他变量