Python cross-platform Program-talking about the relative path and absolute path of Python [zz]

Source: Internet
Author: User

Python cross-platform Program-talking about the relative path and absolute path of Python [zz]

Reprinted please indicate the source http://blog.csdn.net/porcupinefinal/ (respect for labor results)

This may be a headache for beginners of Python, but it may have been a commonplace for laruence. As there is not much information about this on the Internet, this dish is mainly prepared for birds, although I am still a bird :). You are also welcome to taste the old birds and give your valuable comments.

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 regard (at least I think so) (the opposite 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 path is involved in the process, the system type is differentiated first, and then processed differently 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 and UNIX are also booming, so we just need to differentiate them. In general, I use an if... Else statements to differentiate, cainiao practices, how do old birds do?

If OS. Name = "NT ":
Windows path ......
Else:
Linux path ......

Section: This method is stupid and not flexible, but it solves the problem that the same program can be used in windows at the same time.And LinuxAnd does not need to be modified.

The second one:This method converts relative paths to absolute paths of different operating systems in a relatively flexible manner. I like this method relatively :). For example: you have written a small system. All files and subfolders are stored in the main folder of F:/school, the main folder contains folders such as class, teacher, student, and log, and 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. The other paths are the same as those in windows. So how should they be the same? Define a global variable path in Main. 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", while that running in Linux is "/home/KKK/school ", it is estimated that everyone knows what I want to do. If you want to access (read and write) the log file in the program, you 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. LogWe generally like to use path + // log // KK. LogBut in LinuxAnd the file path is not found.

Note: SYS. Path. append (PATH) is added to the main program on the Internet)And then use the relative path to access the file in the system, but I tried it as if there was a problem (you can try it and let me share your labor results as well :))

Related Article

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.