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