Python fragmentation (9): OS. path. dirname (_ file __)

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.