Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python in os.path Magical     - Android determine the device network connection status, and determine the connection (Programming)

- U disk to install Windows and Ubuntu 15.04 dual system (Programming)

- CentOS 7 repair MBR and GRUB (Linux)

- C ++ 11 feature: auto keyword (Programming)

- Linux Security and Optimization (Linux)

- Protobuf compiled and used on the Ubuntu 14.04 (Programming)

- rsync server set up (Server)

- Git build a team development environment operating drills (Linux)

- JQuery implements the same content merge cells (Programming)

- CentOS 6.6 permanent method to modify the DNS address (Linux)

- Use smem visual display Linux memory usage (Linux)

- Depth understanding of Python character set encoding (Programming)

- Inject script commands manually annotated summary (Linux)

- Build ftp server under CentOS 6.5 (Server)

- crontab task scheduling Health Check (Linux)

- Detailed Linux network security policies and protection measures (Linux)

- Large computer network security policy Experience (Linux)

- 10 Codes of good practice PHP (Programming)

- Samhain: Powerful intrusion detection system under Linux (Linux)

- Linux dual physical network card set to a virtual NIC (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 and hardware (Linux)
- Linux pwd command learning experience (Linux)
- The security administrator network analysis tools SATAN Introduction under Linux (Linux)
- Oracle View Object Information (Database)
- Linux Thread Synchronization (Programming)
- CentOS 6.5 configuration SSDB 1.8.0 (Server)
- JBPM6 Tutorial - Fast Fun JBPM table (Linux)
- Access.log Apache access log analysis and how to import it into MySQL (Server)
- To access an Oracle database using Instant Client (Database)
- How Linux system password security guarantee (Linux)
- Use Vagrant build cross-platform development environment for Python (Server)
- dd command: do hard disk IO performance test (Linux)
- Kickstart automated installation and deployment RHEL 7.0 (Linux)
- CentOS 6.5 dual card configuration, one of the external network, a local area network connection (Linux)
- Linux crontab (Linux)
- Briefly explain the invasion of the four-level denial of service attack DoS Linux server (Linux)
- Redis data types Introduction (Database)
- Read and write files efficiently from Apache Kafka (Server)
- How to identify memory leaks in Java (Programming)
- Ubuntu 15.04 Linux install anti-virus software Antiviral 0.2 (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.