我需要处理从db对象生成的一些文件,并且在必需的进程之后需要用files删除该目录。我已经决定使用python templefile包。我试一试,但坚持Direcotry not Empty [ Error 66 ].
在 views.py
中def writeFiles(request, name):
tmpdir = tempfile.mkdtemp()
instance = request.user.instances.get(name=name)
print(instance)
print(instance.name)
code = instance.serverFile
jsonFile = instance.jsonPackageFile
docker = """
FROM node
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/ap
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "node", "server" ]"""
# Ensure the file is read/write by the creator only
saved_umask = os.umask(0o077)
server = 'server.js'
json = 'package.json'
path = os.path.join(tmpdir)
print(path)
try:
with open(path + '/dockerfile', "w") as dockerfile:
dockerfile.write(docker)
with open(path + '/server.js', "w") as server:
server.write(code)
with open(path + 'package.json', "w") as json:
json.write(jsonFile)
print(os.path.join(tmpdir, json))
except IOError as e:
print('IOError:', e)
else:
os.remove(path)
finally:
os.umask(saved_umask)
os.rmdir(tmpdir)
答案 0 :(得分:1)
我会注意到path = os.path.join(tmpdir)
使path
等于tmpdir
。也就是说,当目录不为空时,os.remove
或os.rmdir
都不起作用。
这些是操作系统调用,不会递归到目录中包含的文件。
所以只需使用
import shutil
shutil.rmtree(tmpdir)