1. Understand OS. path. dirname (_ file __)
Obtain the path name:OS. path. dirname ()
Obtain the complete path of the directory where the file is located: OS. path. dirname (_ file __)
You can also configure settings. py in the django project as follows:
For example, configure the database:
1 import os2 DATABASE_ENGINE = 'sqlite3 '3 DATABASE_NAME = OS. path. join (OS. path. dirname (_ file _), 'myapp/mydata. db ')
Among them, OS. path. the dirname (_ file _) function is used to retrieve settings. position of the folder where py is located. path. the join () function matches this position with the specified 'myapp/mydata. the db' string is connected together to implement the sqlite3 database file mydata. the location where the database is stored.
If you edit the following code in eclipse, enter the complete path of the file:
However, if we edit it in the built-in python IDLE or Python command line, the following error will occur:
I was surprised. I thought I had made a mistake, but I wrote it several times. I 'd better google it and find the following answer:
The above is clear. I will not describe it here.
Read: python file operations: http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html