创建一个文件夹,然后根据需要保存任意数量的.txt文档


import os
path = os.getcwd()
print(path)
y = False
while y == False:
dirName = input("Folder name: ")
try:
# Create target Directory
os.mkdir(dirName)
print("Directory '" , dirName ,  "' Created ")
y = True
except FileExistsError:
print("Directory " , dirName ,  " already exists")

folderDir = os.path.join(path, dirName)

y = int(input("Number of profs: "))
for a in range(y):
with open("number.txt","r") as z:
num = z.read()
num = int(num)
newNum = (int(num)+1)
numProf = ("profile"+str(newNum))
with open("number.txt","w") as file:
file.write(str(newNum))
file.close()
with open(folderDir+numProf+".txt","x") as f:
print(numProf)
###THIS DELETES THE FILES
##RESET FILE NUMBER
reset = input(str("Delete all files (Y/N)?"))
if reset == "y" or reset == "Y":
delete = True

if reset == "n" or reset == "N":
delete = False

if delete == True:
with open("number.txt","w") as file:
file.write("0")
file.close()
notRemoved = 0
removed = 0
for x in range(0,101):
try:
os.remove("profile"+str(x)+".txt")
removed += 1
except:
notRemoved +=1
print("Deleted "+str(removed)) 

这是代码^数字是用来记录有多少文件的。我不知道如何打开number来检查目录中有多少内容,这些内容更改并保存在与number.txt相同的文件中,同时仍然创建一个新的空文件夹。

folderDir = os.path.join(path, dirName,numProf)
with open(folderDir+".txt","x") as f:
print(folderDir+numProf)

更改了代码,它就起作用了

相关内容

  • 没有找到相关文章