Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python in os.path Magical     - C ++ in the elimination Wunused (Programming)

- Partition and file system under Linux (Linux)

- How to Install 3.16.7 CKT2 kernel in Ubuntu 14.10, Ubuntu 14.04 and its derivative versions (Linux)

- List Leaves (Programming)

- JavaScript function closures Quick Start (Programming)

- Some problems and countermeasures Linux system calls exist (Linux)

- MongoDB query timeout exception SocketTimeoutException (Database)

- Simple to install Tor browser to Ubuntu 14.04 and Linux Mint 17 (Linux)

- Oracle to read and modify the data block process (Database)

- Repair Chrome for Linux is (Linux)

- Java 8 Lambda principle analysis (Programming)

- Red Hat Enterprise Linux 6.4 Configuring VNC Remote Desktop login access (Linux)

- How to configure a development environment elegant Lua (Linux)

- Nonstandard IMP-00010 error processing one case (Database)

- File SUID, SGID, Sticky property (Linux)

- Elaborate .NET Multithreading: Concepts (Programming)

- Installation Elementary OS Freya 20 things to do (Linux)

- Zabbix installation and configuration process (Server)

- SSH Filesystem use a secure connection for network file system (Linux)

- Ubuntu Install OpenSSL (Linux)

  Python in os.path Magical
  Add Date : 2018-11-21      
  1. Basics

    When os.path set path to the file in a different environment, the role is very large, we often are in Django or Flask see its shadow, in fact, there are several commonly used methods below:

Common method of action
os.path.dirname (__ file__) Returns the current execution path python script execution (see example below), there is a fixed parameter __file__
os.path.abspath (file) returns the absolute path to a file in the current environment, where a parameter file
os.path.join (basedir, file) the file path to the path of the file where the basedir here fbasedir and file all parameters
    OK, we might look at the following example.

2. Test

    Look at the two python script file my current environment:

xpleaf @ leaf: ~ / Source_Code $ pwd
/ Home / xpleaf / Source_Code
xpleaf @ leaf: ~ / Source_Code $ ls
hello.py test_os_path.py
    hello.py there is no content will be used for testing, the main look test_os_path.py code:

import os
path1 = os.path.dirname (__ file__)
print 'The path1 is:', path1
path2 = os.path.abspath (path1)
print 'The path2 is:', path2
path3 = os.path.join (path2, 'hello.py')
print 'The path3 is:', path3
    By looking at the implementation of the following two ways, we come to a deep understanding of the role of the above three methods:

(1) by way of a relative path is performed test_os_path.py

xpleaf @ leaf: ~ / Source_Code $ python test_os_path.py
The path1 is:
The path2 is: / home / xpleaf / Source_Code
The path3 is: /home/xpleaf/Source_Code/hello.py
(2) in a manner to perform absolute path test_os_path.py

xpleaf @ leaf: ~ / Source_Code $ python /home/xpleaf/Source_Code/test_os_path.py
The path1 is: / home / xpleaf / Source_Code
The path2 is: / home / xpleaf / Source_Code
The path3 is: /home/xpleaf/Source_Code/hello.py
    Through the implementation of the above two ways of output, it is easy to see the role of the three. That in the actual development, what use is it?

3. Use os.path in the actual development

    In the actual development, we certainly want to set a path to certain files, such as Web development, for the path templates and static files and other settings, in fact, if you've used Django or Flask, you should be able to see often that in their configuration files, there os.path appears generally to use this:

(1) First get the path of the current file (such as configuration files) are located

basedir = os.path.abspath (os.path.dirname (__ file__))
Absolute path (2) to set a file

static_file_path = os.path.join (basedir, 'index.html')
    Of course, os.path usage also have a lot more, just a list of three common here, and given the general usage of the development environment, as to whether this have to use, completely depends on each person's own ideas and methods, where only for reference.
- Python is not C (Programming)
- Ubuntu installed racing game Speed Dreams 2.1 (Linux)
- The REVERSE function of DB2 (Database)
- Bash variable expansion modifier (Programming)
- Questions about Linux compiler u-boot (Programming)
- Update GAMIT10.6 command (Linux)
- Oracle 11g RAC installation considerations Miscellany (Database)
- Installation GitLab appears ruby_block supervise_redis_sleep action run (Linux)
- Database start listening TNS-12537, TNS-12560 error (Database)
- CentOS7 install and configure Nagios (Server)
- Use Python to perform system commands common usage (Programming)
- MySQL / MariaDB database master-slave replication (Database)
- Recycle Bin function realization in Linux (Linux)
- Linux Basics Tutorial: create your own Vim IDE (Linux)
- Download Manager uGet 2.0 installed in Debian, Ubuntu, Linux Mint and Fedora (Linux)
- After SSH change the default port, how to use Git (Linux)
- Ruby and Python simple comparison (Programming)
- Using Libreoffice under ubuntu (Linux)
- Linux see whether there is a hacker program (Linux)
- Linux SVN account password to save your settings (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.