iOS 開發(fā)中 python 腳本常用模塊和函數(shù)

os

  1. 獲取當前路徑 : os.getcwd()
  2. 獲取某個目錄下所有文件:os.listdir(file_path)
  3. 根據(jù)字符串創(chuàng)建目錄:os.makedirs(local_pod)
  4. 獲取系統(tǒng)Home目錄路徑:os.environ['HOME']
  5. 切換目錄:os.chdir(example_path)
  6. 執(zhí)行終端命令(shell、git、pod 等):os.system('sh pod_framework_mode.sh')

7.從一個命令打開一個管道:current_repo_current_branchs = os.popen('git symbolic-ref --short -q HEAD').readlines()

  1. 刪除文件:os.remove(current_file_path)
  2. 通過在目錄樹中游走輸出在目錄中的文件名,向上或者向下: for root, dirs, files in os.walk(file_dir):

os.path

  1. 將一個路徑拆分為 名字 和 后綴:(name, ext) = os.path.splitext(file),if ext == '.podspec'
  2. 將多個字符串拼接在一起組成一個路徑:example_path = os.path.join(current_path, main_module_name, 'Example')
  3. 判斷某個目錄是否存在:if os.path.exists(local_pod) is not True:
  4. 判斷某個路徑是否是文件:if os.path.isfile(file_or_folder):
  5. 判斷某個路徑是否是文件夾:if os.path.isdir(file_or_folder):

sys

  1. 獲取執(zhí)行命令時帶的參數(shù):args = sys.argv[1:]
  2. 系統(tǒng)推出:sys.exit()

string

  1. 判斷一個字符串是否包含一個子字符串:if ext == '.podspec' and string.find(file, 'Protocol') == -1:
  2. 將字符串轉為大寫或小寫:if string.lower(name) == 'y' or string.lower(name) == 'yes' or name == '':
  3. 判斷一個字符串是否包含一個子字符串:if string.find(line, module_name) != -1:

shutil

  1. 復制文件到目的目錄:shutil.copy(file_or_folder, destination)
  2. 將目錄下文件全部刪除:shutil.rmtree(destination)
  3. 復制文件夾:shutil.copytree(file_path, current_file_path)
  4. 復制文件:shutil.copyfile(file_path, current_file_path)

from distutils.dir_util import copy_tree:

  1. 將某個路徑下所有文件復制到另外一個路徑下:copy_tree(path, backup_path)

datetime

  1. 獲取當前時間:current_date = datetime.now()
  2. 將當前時間轉為相應格式:date_str = current_date.strftime('%y年%m月%d日')

subprocess

  1. 關閉 Xcode :subprocess.call(['osascript', '-e', 'tell application "Xcode" to quit'])

系統(tǒng)內置

  1. 獲取控制臺輸入字符串: name = raw_input(bcolors.WARNING + '確認要拷貝代碼到git目錄下?\n確定?請輸入(Y/N)\n' + bcolors.ENDC)
  2. 打印字符串:print(bcolors.HEADER + '開始執(zhí)行注入模式>>>>\n' + bcolors.ENDC)
  3. 打開文件:open(pod_file, 'r')
  4. 讀取文件內容:file_handler.readlines()
  5. 將內容寫到文件里面去:out.writelines(content)
  6. 獲取數(shù)組指定元素index:index = content.index(line)
  7. 查找字符串里面指定字符串位置:path_index = str.find('path =>')
  8. 查找字符串指定位置后面某個字符串的index:module_start = str.find(''', path_index) + 1
  9. 根據(jù)兩個index獲取子字符串:module_path = str[module_start: module_end]
    10.數(shù)組元素個數(shù):if len(current_repo_current_branchs) > 0:
    11.將字符串以某個字符為間隔轉為數(shù)組:remotes_origin_branch_arr = remotes_origin_branch.split('/')
  10. 字符串替換某個字符為其他字符:branch = remotes_origin_branch_arr[2].replace('\n', '')
  11. 數(shù)組后面追加元素:manifest_all_branch.append(branch.strip())
  12. 移除字符串頭尾指定的字符(默認為空格或換行符)或字符序列:if manifest_branch.strip() not in manifest_all_branch:
  13. 刪除數(shù)組元素:del list[len(list) - 1]
  14. 數(shù)組替換:file_contents=file_contents.replace(old_import, new_import)

Git 相關

  1. 獲取當前倉庫當前分支:git symbolic-ref --short -q HEAD
  2. git 倉庫根目錄:git rev-parse --show-toplevel
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,932評論 0 13
  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 6,353評論 0 10
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,259評論 0 38
  • 常用模塊 認識模塊 什么是模塊 什么是模塊? 常見的場景:一個模塊就是一個包含了python定義和聲明的文件,文...
    go以恒閱讀 2,171評論 0 6
  • 模塊和包 一 模塊 1 什么是模塊? 常見的場景:一個模塊就是一個包含了python定義和聲明的文件,文件名就是...
    go以恒閱讀 2,349評論 0 4

友情鏈接更多精彩內容