Obtain the current path through Python Programming

Source: Internet
Author: User
Obtain the current path through Python Programming


When writing various applications, obtaining the current path has always been a classic problem. Only by obtaining the current path can we further find all other files in the same path. The Python programming language can also obtain the current path through some system calls.

The module provided in this Article can not only obtain the current path, but also form a URI string used by sqlobject to synthesize the database path with the specified file name in the current path.

The following code is available for your research:

#-*-Coding: GBK -*-
# File: curpath. py
# Date: 2006-12-23
# Authro: gashero
# Copyright @ 1999-2006, Harry gashero Liu.

"""
An idiot Module
Used only to obtain the current path
"""

Import OS

_ Localdir = OS. Path. dirname (_ file __)
_ Curpath = OS. Path. normpath (OS. Path. Join (OS. getcwd (), _ localdir ))
Curpath = _ curpath

# Dbfullpath = OS. Path. Join (curpath, sqlitedbfilename)

Def getsqlobjecturi (dbfilename ):
"Gets the connection string of sqlobject in the current path, in Uri format """
Global curpath
Dbpath = curpath. Replace (":", "|"). Replace ("//","/")
Return Unicode ("SQLite:/" + dbpath + "/" + dbfilename, "GBK"). encode ("UTF-8 ")
 
To a line:
Curpath = OS. Path. normpath (OS. Path. Join (OS. getcwd (), OS. Path. dirname (_ file __)))
You can:
Fullpath = OS. Path. Join (curpath, filename)

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.