Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python in os.path Magical     - Linux VMware virtual machine after the cloning of the card can not start to solve (Linux)

- Ubuntu dual-card system configuration method (Server)

- Tsung first test installation (Linux)

- Apache2.4.7 make an error [exports.lo] Error 1 Solution (Server)

- Ubuntu 14.04 after the restart the default maximum screen brightness solutions (Linux)

- How to understand the difference between synchronous and asynchronous non-blocking blocking (Programming)

- CentOS 6.4 (64bit) install Python 2.7.5 (Linux)

- MySQL performance comparison of large amounts of data storage (Database)

- React Getting Started Tutorial (Linux)

- installation process of Matlab 2012a under Ubuntu 14.04 (Linux)

- Android Launcher3 Application List Modify a transparent background (Linux)

- Installation and configuration under Linux SVN server - Backup - Recovery - Scheduled Tasks (Server)

- Use dump restore mode fast backup and recovery system FreeBSD (Linux)

- Ubuntu security settings and tools (Linux)

- MySQL binlog group to submit XA (two-phase commit) (Database)

- DIY security of Linux platform (Linux)

- Linux user status query, and to unlock locked user (Linux)

- Install JDK 1.7 + Eclipse in CentOS 6.4 in (Linux)

- SQL MySQL query table duplicate data (Database)

- Using RAID in Linux: Create a RAID 5 (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.
- Linux Fundamentals of the text, data flow processing orders (Linux)
- Between the two to achieve the main MySQL database synchronization from (Database)
- Build a Linux development environment under STC89C52RC (Linux)
- Installation Atom text editor on Mint Ubuntu / Linux (Linux)
- Linux Monitoring Command (Linux)
- Oracle 11g through SCN do incremental backup repair standby library detailed process (Database)
- Use Visual Studio 2015 to develop Android program (Programming)
- Ubuntu 14.04, 13.10 install OpenCV 2.4.9 (Linux)
- Oracle 10g New Features - Archive Compression (Database)
- Erlang concurrency and foundation (Programming)
- Linux operating system set up to effectively prevent ARP attacks (Linux)
- 11 examples in Linux df command (Linux)
- To get Java class / jar package path (Programming)
- C # Future: Method Contract (Programming)
- Shell scripts to copy all directories under the current directory of a certain type of file to the same directory (Linux)
- Monitor traffic Linux Shell Edition (Programming)
- Linux prohibit non-WHEEL user su command Detail (Linux)
- Automatic Clear date directory shell script (Linux)
- Based shell: using read, the command-line script to achieve mass participation and input two integer calculation (Programming)
- Laravel 4 Expansion Pack (Server)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.