Python學習筆記之os模組使用總結_python

來源:互聯網
上載者:User

複製代碼 代碼如下:

#!/usr/bin/env python
##-*- coding: utf-8 -*-
 
import os
 
print "n歡迎大家跟我一起學Python";
 
system=os.name;                                                                #擷取系統的類型
if(system=="nt"):
        print "您使用的作業系統是windows";
        print "使用windows表示的特定路徑分割符是 "+os.sep;                #擷取系統的分隔字元
        print "您的電腦系統的終止符效果"+os.linesep;                        #擷取系統分行符號
else:
        print "您使用的作業系統是Linux";
        print "使用windows表示的特定路徑分割符是 "+os.sep;
        print "您的電腦系統的終止符是"+os.linesep;
 
path=os.getcwd();                                                        #獲得目前的目錄
print "您運行本程式所在目錄是 "+path;
 
print "你電腦的Path環境變數為 "+os.getenv("Path");                        #擷取環境變數的值os.putenv(key,value)可以設定環境變數的值
 
print "你當前檔案夾中的檔案有:";
print os.listdir(path);                                                        #擷取檔案夾中的所有檔案
if(os.path.exists("test.txt")):                                                #判斷檔案是否存在
        os.remove("test.txt")                                                #刪除指定檔案
        print "n刪除成功";
else:
        print "n檔案不存在";
print "咱們來刪除一個檔案,刪除後的結果:";
print os.listdir(path);                               
 
print "n查看您的ip:n";
print os.system("ipconfig");                                                #執行系統命令
 
filepath1="C:Python27";
filepath2="C:Python27os.py";
 
if(os.path.isfile(filepath2)):                                                #判斷是不是檔案
        print filepath2+"是一個檔案";
if(os.path.isfile(filepath1)):
        print filepath1+"是一個檔案";
else:
        print filepath1+"不是一個檔案";
 
name="os.py";
print "本程式的大小為";
print os.path.getsize(name);                                                #擷取檔案大小
name=os.path.abspath(name);                                                #擷取檔案的絕對路徑
print "本程式的絕對路徑是"+name;               
 
 
print "本程式的路徑的檔案名稱分別為:";
print os.path.split(name);                                                #將檔案名稱和路徑分開
 
files=os.path.splitext(name);                                                #將檔案名稱和擴充分開
print "本程式的擴充為"+files[1];
 
print "本程式的檔案名稱為"+os.path.basename(name);#擷取檔案的名字
 
print "本程式的路徑為"+os.path.dirname(name);#擷取檔案的路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.