Python Learning Notes OS module Usage Summary _python

Source: Internet
Author: User

Copy Code code as follows:

#!/usr/bin/env python
##-*-Coding:utf-8-*-

Import OS

Print "N Welcome everyone to learn Python with me";

System=os.name; #获取系统的类型
if (system== "NT"):
Print "The operating system you are using is Windows";
The specific path delimiter that print "uses Windows to represent is" +OS.SEP; #获取系统的分隔符
Print "Terminator effect for your computer system" +OS.LINESEP; #获取系统换行符
Else
Print "The operating system you are using is Linux";
The specific path delimiter that print "uses Windows to represent is" +OS.SEP;
Print "The Terminator of your computer system is" +OS.LINESEP;

PATH=OS.GETCWD (); #获得当前目录
Print "The directory where you run this program is" +PATH;

Print "Your computer's PATH environment variable is" +os.getenv ("path"); #获取环境变量的值os. Putenv (Key,value) to set the value of an environment variable

Print "The files in your current folder are:";
Print Os.listdir (path); #获取文件夹中的所有文件
if (os.path.exists ("Test.txt")): #判断文件是否存在
Os.remove ("Test.txt") #删除指定文件
Print "n Delete succeeded";
Else
Print "N file does not exist";
Print "Let's delete a file and delete the result:";
Print Os.listdir (path);

Print "N View your Ip:n";
Print Os.system ("ipconfig"); #执行系统命令

Filepath1= "C:python27";
Filepath2= "c:python27os.py";

if (Os.path.isfile (filepath2)): #判断是不是文件
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 size of this program is";
Print os.path.getsize (name); #获取文件大小
Name=os.path.abspath (name); #获取文件的绝对路径
Print "The absolute path of this program is" +NAME;


Print "The path of this program filename is:";
Print os.path.split (name); #将文件名和路径分开

Files=os.path.splitext (name); #将文件名和扩展分开
Print "The extension of this program is" +files[1];

Print "The file name for this program is +os.path.basename (name); #获取文件的名字

Print "The path to this program is" +os.path.dirname (name); #获取文件的路径

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.