Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Python in os.path Magical     - Windows 7 hard disk installation notes Debian (Linux)

- LNMP summary of the issues common 502 Bad Gateway (Server)

- How to install Ubuntu strategy game Wesnoth 1.12.0 (Linux)

- MySQL Online DDL tools of pt-online-schema-change (Database)

- Configuring a Linux operating system against syn attack (Linux)

- Linux systems use logwatch log file monitoring (Linux)

- Fedora 22 how to play rmvb mp4 mkv video files (Linux)

- How to use the character in C ++ without pressing the Enter key to enter the Show (Programming)

- Hadoop2.6.3 build clusters and the development of MapReduce WIN7 by Eclipse on Linux demo (Server)

- CentOS6.5 install SVN & visual management tools iF.SVNAdmin (Server)

- OpenGL Superb Learning Notes - Fragment Shader (Programming)

- Teach you how to protect the security of Linux desktop (Linux)

- Apache Tomcat integration and resin (Server)

- To add the Oracle JDBC driver in Maven repository (Linux)

- How to create a cloud encrypted file system in Linux systems (Linux)

- VMware 8 installation configuration Win7, CentOS-7 Wizard (Linux)

- In-depth understanding of PHP ini configuration (Server)

- Physical backup and recovery SYSTEM table space (Database)

- Shell script to crawl through AWR SQL Report Problems (Database)

- MySQL various log summary (Database)

 
         
  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

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

1
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.
     
         
         
         
  More:      
 
- Getting Started with Linux system to learn: how to configure a static IP address for CentOS7 (Linux)
- Linux System Getting Started Learning: The Linux anacron command (Linux)
- Java Graphics2D Paint (Programming)
- Alien Magic: RPM and DEB Mutual Convert (Linux)
- Linux server data backup (Server)
- How to use OpenVPN and PrivacyIDEA build two-factor authentication for remote access (Server)
- To use Linux to create a secure managed gateway (Linux)
- How to migrate MySQL to MariaDB under linux (Database)
- Hadoop2.4.0 Eclipse plug-in making (Server)
- Git Experience Sharing - Using a remote repository (Linux)
- Ubuntu 14.10 Install Ubuntu Touch Music App 2.0 (Linux)
- Linux find command to find files (Linux)
- Debian (Wheezy) were installed wxPython GUI development (Linux)
- Linux Regular expressions grep and egrep (Linux)
- Swift acquaintance of values and references, the circular reference, Agent Precautions (Programming)
- MySQL Error Code Complete (Database)
- Linux Basic Course: Install the software from source code (Linux)
- Offline (local) Yum source structures (Linux)
- How to install Gnome 3.10 in Ubuntu 13.10 (Linux)
- Using Android Studio integrated development environment to build Android (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.