Detailed description of relative paths and absolute paths for getting started with Python

Source: Internet
Author: User

If you are a beginner of Python and have some knowledge about the operation solutions of relevant applications, you can click the following articles to have a better understanding of the Python entry, the following is an introduction to the relevant content. I hope you will get some benefits.

This may be a headache for beginners of Python, but it may have been a commonplace for laruence. because there is not much information about this on the Internet, this dish is mainly prepared for the birds, although I am still a bird. you are also welcome to give your valuable suggestions.

  • Introduction to the advantages of python script in practical application
  • How to Build Python Data Structures
  • Introduction to the implementation of the Python Programming Language
  • Basic concepts of Python syntax
  • Detailed description of the application solution for creating Python Data Structures

Python is a cross-platform language, but it does not mean that a program you write in Python can run on different platforms. the relative path and absolute path are typical in this aspect (the relative path is feasible in Windows, but the corresponding file path cannot be found in Linux ), because the structure of Linux and Windows is quite different, the difference also makes it difficult to write programs on the Expansion platform. I have tried two ways to solve this problem. I will share it with you.

The first one: When a path is involved in The process, The system type is distinguished first, and then different processing is performed based on different operating systems. python currently supports the following operating systems: 'posix', 'nt ', 'mac', 'oss2', 'ce', 'java', 'riscos ', I have never tried it completely, but currently the mainstream application systems are Windows, and Linux is also booming, so we just need to differentiate them. generally, I use an if... Else statements to differentiate, cainiao practices, how do old birds do?

 
 
  1. if os.name == ”nt” 

Windows path... ElseLinux path... Section: This method is stupid and not flexible, but it solves the problem that the same program can run in both Windows and Linux without modification.

The second one: This method is relatively flexible to convert relative paths to absolute paths of different operating systems. I prefer this method. for example: you have written a small system. All files and subfolders are placed in the main folder of f: \ school. The main folder contains class, teacher, student, log and other folders, including main. py is the main file of this small system, and there are some subfiles. py file.

The path of the main folder in Linux may be/home/kkk/school. Other relative paths are the same as those in Windows. How can we unify them? In main. define a global variable path in py and assign it to path = OS. path. abspath (OS. path. dirname (sys. argv [0]). The value of this path running in Windows is "f:/school ", in Linux, the value of running it is "/home/kkk/school". It is estimated that everyone knows what I want to do. If you want to access (read/write) in the program) log files can use path + "/log/kk. log to indicate the file path.

Section: This method is more flexible than the first method. Note that path + "/log/kk. log ", we generally like to use path + \ log \ kk. log method, but this method does not work in Linux, prompting that the file path cannot be found. note: You can also add sys to the main program on the Internet. path. append (path), and then access the file in the system using the relative path method, but I tried it as if there was a problem (you can try it and let me share your work results) website construction.

The above article describes the relative paths and absolute paths for getting started with Python.
 

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.