導航:首頁 > 編程語言 > python處理git

python處理git

發布時間:2022-07-23 10:36:05

1. python如何使用gitbash執行git命令

代碼如下:

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

@name : find_t.py# @author : cat#

@date : 2017/8/2.import osimport timedef bash_shell(bash_command):
"""
python 中執行 bash 命令 :param bash_command:
:return: bash 命令執行後的控制台輸出
"""
try:

return os.popen(bash_command).read().strip()

except: return Nonedef find_target(target_path="./../", key='.git'):
"""

查找目標目錄所在的目錄 : 如

/aa/bb/.git --> return /aa/bb/
:param target_path:
:param key: target
:return:
"""
walk = os.walk(target_path)

for super_dir, dir_names, file_names in walk:

for dir_name in dir_names:

if dir_name == key:

dir_full_path = os.path.join(super_dir, dir_name)

# print(dir_full_path, super_dir, dir_name, sep=" ## ")

yield super_dirif __name__ == '__main__':

print("start execute bash ...........")

st = time.time()
cwd = os.getcwd()

# this for repo
f

or repo_path in find_target(os.getcwd(), key='.repo'):

os.chdir(repo_path)

if repo_path == os.getcwd():

print('find repo in -->', repo_path)

print(bash_shell('pwd'))

print(bash_shell('repo forall -c git config core.fileMode false --replace-all'))

else:

print('error in chdir 2 {}'.format(repo_path))

if os.getcwd() != cwd:

os.chdir(cwd)

if os.getcwd() != cwd:

print('change 2 cwd FAIL !!! {}'.format(cwd))

# this for git

for git_path in find_target(os.getcwd(), key='.git'):

os.chdir(git_path)

if git_path == os.getcwd():

print('find git in -->', git_path)

print(bash_shell('pwd'))

print(bash_shell('git config --global core.filemode false'))

else:

print('error in chdir 2 {}'.format(git_path))

if os.getcwd() != cwd:

os.chdir(cwd)

if os.getcwd() != cwd:

print('change 2 cwd FAIL !!! {}'.format(cwd))

et = time.time()

print('

#### execute finished in {:.3f} seconds ####'.format(et - st))

print(' ') # test for bash_command
# print(bash_shell('git init'))

# print(bash_shell('ls -al'))

2. 關於 Git 和 python 的問題:使用limejs的過程中要使用python去編譯,並且要用Git下載一些東西,出錯如下:

are you trying to run a linux program on windows?
"
D:\Program Files\Git/libexec/git-core\git-svn
"
is not a valid windows path.

3. gitpython如何修改文件內容不影響格式

三種方法

_弧⑿薷腦募絞?

_ef alter(file,old_str,new_str):

?

?"""

_婊晃募械淖址?

?:param file:文件名

?:param old_str:就字元串

?:param new_str:新字元串

?:return:

?

?"""

_ile_data = ""

?

_ith open(file, "r", encoding="utf-8") as f:

?

_or line in f:

?

_f old_str in line:

?

_ine = line.replace(old_str,new_str)

?

_ile_data += line

?

_ith open(file,"w",encoding="utf-8") as f:

?

_.write(file_data)

?

_lter("file1", "09876", "python")

?

__言募諶鶯鴕薷牡哪諶菪吹叫攣募薪寫媧⒌姆絞?

?

?2.1 python字元串替換的方法,修改文件內容

?

_mport os

?

_ef alter(file,old_str,new_str):

?

?"""

?

_婊壞淖址吹揭桓魴碌奈募校緩蠼募境攣募奈次募拿?

?

?:param file: 文件路徑

?

?:param old_str: 需要替換的字元串

?

?:param new_str: 替換的字元串

?

?:return: None

?

?"""

?

_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:

?

_or line in f1:

?

_f old_str in line:

?

_ine = line.replace(old_str, new_str)

?

_2.write(line)

?

_s.remove(file)

?

_s.rename("%s.bak" % file, file)

?

_lter("file1", "python", "測試")

?

?2.2 python 使用正則表達式 替換文件內容 re.sub 方法替換

?

_mport re,os

?

_ef alter(file,old_str,new_str):

?

_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:

?

_or line in f1:

?

_2.write(re.sub(old_str,new_str,line))

?

_s.remove(file)

?

_s.rename("%s.bak" % file, file)

4. python通過git push上傳文件

你可以使用subpocess直接調用git命令就可以了。

importshlex
importsubprocess
#push本地的test分支到伺服器上的master分支
cmd="gitpushorigintest:master"
cwd="你項目的路徑"
subprocess.check_output(shlex.split(cmd),cwd=cwd)
如果解決了您的問題請採納!

如果未解決請繼續追問

5. 怎麼用python去git commit

git是分為三部分,一部分是你自己的文件,另外一個是緩存區,最後一個是本地庫。當你修改了自己的文件後,你會git add xx將修改保存到緩存區,然後再用commit推送修改到本地庫中。

6. python怎麼直接操作git mvn命令

一般這類對象文件和存檔文件都是編譯過程中出現的,我們用不著跟蹤它們的版本。第二行告訴Git忽略所有以波浪符(~)結尾的文件,許多文本編輯軟體(比如Emacs)都用這樣的文件名保存副本。此外,你可能還需要忽略log,tmp或者pid目錄,以及自動生成的文檔等等。要養成一開始就設置好.gitignore文件的習慣,以免將來誤提交這類無用的文件。

7. python處理git日誌文本

數據量有多大,可以有償幫你處理

8. python能使用gitgunicorn不能

可以使用。
python能使用git,gunicorn。國外的大家都在使用github,很多的開源項目都在github上面,Python使用gunicorn部署Flask項目。
Python由荷蘭數學和計算機科學研究學會的吉多·范羅蘇姆於1990年代初設計,作為一門叫做ABC語言的替代品。

閱讀全文

與python處理git相關的資料

熱點內容
java常用的伺服器 瀏覽:275
集結APP在哪裡下載 瀏覽:798
歐洲cf玩什麼伺服器 瀏覽:527
如何連接另一台電腦上的共享文件夾 瀏覽:679
如何讓桌面文件夾搬家到e盤 瀏覽:71
java自動格式化 瀏覽:617
ipad怎麼查看文件夾大小 瀏覽:581
手工粘土解壓球 瀏覽:550
在線視頻教育源碼 瀏覽:39
快四十學什麼編程 瀏覽:754
gnumakelinux 瀏覽:537
視易峰雲伺服器怎麼改系統 瀏覽:535
javamap取值 瀏覽:768
mac和win磁碟加密軟體 瀏覽:474
蘋果為什麼會連接不到伺服器 瀏覽:726
pdf格式文件如何保存 瀏覽:303
小霸王伺服器tx什麼意思 瀏覽:75
解釋dns命令 瀏覽:584
dmx512怎麼編程 瀏覽:744
北京雲主機17t雲伺服器 瀏覽:232