如何在django中使用shell命令在目录中创建文件

问题描述:

Iam在django中做一个项目,我必须连接一些日志文件并将它放在新文件中。如何在django中使用shell命令创建一个文件?如何在django中使用shell命令在目录中创建文件

+0

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files –

Django,只是一个Python框架。所以,你可以在Django中做什么,你可以在Python中做什么。

在Python中,打开(和创建)文件的方式是open(filename, mode)

>>> f = open('testing', 'w') 

之后,你有一个文件对象,你可以写在上面,并做你需要做的一切后,关闭它。

>>> f.write('Hello world!') 
>>> f.close() 
>>> f = open('testing', 'r') 
>>> f.read() 
'Hello world!'