我在类中的函数中创建文件,文件保存在与代码文件相同的路径中,但我想将它们保存到不同的文件夹中,
save_path = raw_input("give save path. like '/home/user/dalbums'")
album = raw_input("the name of album: ")
completeName = os.path.join(save_path,album)
if not os.path.exists(completeName):
os.makedirs(completeName)
class X:
def saver(my, info):
with open(completeName +'/file2/save.txt','a') as f:
for i in info:
f.write(info)
lo = info[0]
with open(completeName +'/file1/txt.txt','a') as f:
for i in lo:
f.write(lo)
答案 0 :(得分:0)
您已确保存储在completeName
中的路径存在于磁盘上...对您的2个子文件夹执行相同的操作:
save_path = raw_input("give save path. like '/home/user/dalbums'")
album = raw_input("the name of album: ")
completeName = os.path.join(save_path,album)
file2_path = os.path.join(completeName , 'file2')
file1_path = os.path.join(completeName , 'file1')
if not os.path.exists(file2_path):
os.makedirs(file2_path)
if not os.path.exists(file1_path):
os.makedirs(file1_path)
class X:
def saver(my, info):
with open(file2_path +'/save.txt','a') as f:
for i in info:
f.write(info)
lo = info[0]
with open(file1_path + '/txt.txt','a') as f:
for i in lo:
f.write(lo)
答案 1 :(得分:0)
您需要创建完整目录:
import os
class X:
def saver(self, info):
path_name = os.path.join(completeName, 'file2')
if not os.path.exists(path_name):
os.makedirs(path_name)
with open(os.path.join(path_name, 'save.txt'), 'a') as f:
for i in info:
f.write(info)
lo = info[0]
path_name = os.path.join(completeName, 'file1')
if not os.path.exists(path_name):
os.makedirs(path_name)
with open(os.path.join(path_name, 'txt.txt'), 'a') as f:
for i in lo:
f.write(lo)