Summary of OS module usage of Python learning notes

  • 2020-04-02 14:20:11
  • OfStack


#!/usr/bin/env python
##-*- coding: utf-8 -*-
 
import os
 
print "n Welcome to study with me Python";
 
system=os.name;                                                                # Gets the type of system
if(system=="nt"):
        print " The operating system you are using is windows";
        print " use windows The specified path separator is "+os.sep;                # Gets the delimiter of the system
        print " Termination effect for your computer system "+os.linesep;                        # Gets the system newline character
else:
        print " The operating system you are using is Linux";
        print " use windows The specified path separator is "+os.sep;
        print " The termination of your computer system is "+os.linesep;
 
path=os.getcwd();                                                        # Get the current directory
print " The directory where you run this program is "+path;
 
print " Your computer's Path The environment variable is "+os.getenv("Path");                        # Gets the value of the environment variable os.putenv(key,value) You can set the value of an environment variable
 
print " The files in your current folder are: ";
print os.listdir(path);                                                        # Gets all the files in the folder
if(os.path.exists("test.txt")):                                                # Determine if the file exists
        os.remove("test.txt")                                                # Delete specified file
        print "n Delete the success ";
else:
        print "n File does not exist ";
print " Let's delete a file, delete the result: ";
print os.listdir(path);                               
 
print "n Look at your ip : n";
print os.system("ipconfig");                                                # Execute system commands
 
filepath1="C:Python27";
filepath2="C:Python27os.py";
 
if(os.path.isfile(filepath2)):                                                # Whether it's a file or not
        print filepath2+" It's a file ";
if(os.path.isfile(filepath1)):
        print filepath1+" It's a file ";
else:
        print filepath1+" It's not a file ";
 
name="os.py";
print " The size of this program is ";
print os.path.getsize(name);                                                # Get file size
name=os.path.abspath(name);                                                # Gets the absolute path to the file
print " The absolute path of this procedure is "+name;               
 
 
print " The file names of the path of this program are: ";
print os.path.split(name);                                                # Separate the file name from the path
 
files=os.path.splitext(name);                                                # Separate the file name from the extension
print " The extension of this program is "+files[1];
 
print " The file name of this program is "+os.path.basename(name);# Gets the name of the file
 
print " The path of this program is "+os.path.dirname(name);# Gets the path to the file


Related articles: