Summary of the use of the OS module in Python learning Notes

Source: Internet
Author: User
This article mainly introduces the usage of the OS module in the Python study notes. This article summarizes multiple common methods. For more information, see the following code:


#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-

Import OS

Print "n welcome everyone to learn Python with me ";

System = OS. name; # obtain the system type
If (system = "nt "):
Print "your operating system is windows ";
Print "the specific path delimiter in windows is" + OS. sep; # obtain the system separator
Print "Terminator effect of your computer system" + OS. linesep; # obtain the system line break
Else:
Print "your operating system is Linux ";
Print "the specific path delimiter in windows is" + OS. sep;
Print "The Terminator of your computer system is" + OS. linesep;

Path = OS. getcwd (); # obtain the current directory
Print "the directory in which you run the program is" + path;

Print "your computer's Path environment variable is" + OS. getenv ("Path"); # get the environment variable value OS. putenv (key, value) you can set the environment variable value

Print "the files in your current folder are :";
Print OS. listdir (path); # obtain all files in the folder
If (OS. path. exists ("test.txt"): # checks whether the file exists.
OS. remove ("test.txt") # delete a specified file
Print "n deleted ";
Else:
Print "n file does not exist ";
Print "let's delete an object. The deleted result :";
Print OS. listdir (path );

Print "n view your ip address: n ";
Print OS. system ("ipconfig"); # run the system command

Filepath1 = "C: Python27 ";
Filepath2 = "C: Python27os. py ";

If (OS. path. isfile (filepath2): # check whether the file is correct.
Print filepath2 + "is a file ";
If (OS. path. isfile (filepath1 )):
Print filepath1 + "is a file ";
Else:
Print filepath1 + "not a file ";

Name = "OS. py ";
Print "the program size is ";
Print OS. path. getsize (name); # get the file size
Name = OS. path. abspath (name); # obtain the absolute path of the object
Print "the absolute path of this program is" + name;


Print "the program path file names are :";
Print OS. path. split (name); # Separate file names from paths

Files = OS. path. splitext (name); # Separate file names from extensions
Print "the extension of this program is" + files [1];

Print "the program file name is" + OS. path. basename (name); # get the file name

Print "the path of this program is" + OS. path. dirname (name); # obtain the path of the file

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.