GVKun编程网logo

在Python脚本中获取当前的git hash(python获取git提交当前版本)

14

关于在Python脚本中获取当前的githash和python获取git提交当前版本的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于os.system()是在Python脚本中获取一组文件

关于在Python脚本中获取当前的git hashpython获取git提交当前版本的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于os.system()是在Python脚本中获取一组文件的最佳方法吗?、从python脚本中获取python脚本的输出、从Python脚本获取当前目录的父目录、你如何在python中获取当前用户和脚本的工作目录?等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

在Python脚本中获取当前的git hash(python获取git提交当前版本)

在Python脚本中获取当前的git hash(python获取git提交当前版本)

我想在Python脚本的输出中包括当前的git哈希(作为生成该输出的代码的 版本号 )。

如何在Python脚本中访问当前的git哈希?

答案1

小编典典

git describe命令是创建代码的人为表示的“版本号”的好方法。从文档中的示例中:

使用git.git之类的当前树,我得到:

[torvalds@g5 git]$ git describe parentv1.0.4-14-g2414721

即我的“父”分支的当前头基于v1.0.4,但是由于它的顶部还有一些提交,describe添加了额外的提交数量(“ 14”)和该提交的缩写对象名称本身(“
2414721”)结尾。

在Python中,您可以执行以下操作:

import subprocesslabel = subprocess.check_output(["git", "describe"]).strip()

os.system()是在Python脚本中获取一组文件的最佳方法吗?

os.system()是在Python脚本中获取一组文件的最佳方法吗?

有一些很好的库可以通过Python本机通过HTTP下载文件,而不是启动外部程序。一种非常流行但功能强大但易于使用的请求称为“请求:https://requests.readthedocs.io/en/master/

如果需要,您必须自己实现某些功能,例如@IBOutlet(尽管您的示例令人困惑,因为您使用--recursive却表示正在下载一个文件)。例如参见recursive image download with requests。

如果需要进度条,可以将另一个名为--recursive的库与请求一起使用。参见Python progress bar and downloads。

如果要下载的文件很大,这是我写的一个答案,该答案显示了如何获得最佳性能(最快tqdm):https://stackoverflow.com/a/39217788/4323。

从python脚本中获取python脚本的输出

从python脚本中获取python脚本的输出

printbob.py:

import sysfor arg in sys.argv:    print arg

getbob.py

import subprocess#printbob.py will always be in root of getbob.py#a sample of sending commands to printbob.py is:#printboby.py arg1 arg2 arg3   (commands are seperated by spaces)print subprocess.Popen([''printbob.py'',  ''arg1 arg2 arg3 arg4'']).wait()x = raw_input(''done'')

我得到:

  File "C:\Python27\lib\subprocess.py", line 672, in __init__    errread, errwrite)  File "C:\Python27\lib\subprocess.py", line 882, in _execute_child    startupinfo)WindowsError: [Error 193] %1 is not a valid Win32 application

我在这里做错了什么?我只想在另一个python脚本中获取另一个python脚本的输出。我需要调用cmd.exe还是可以只运行printbob.py并向其发送命令?

答案1

小编典典
proc = subprocess.Popen([''python'', ''printbob.py'',  ''arg1 arg2 arg3 arg4''], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)print proc.communicate()[0]

不过,必须有一种更好的方法,因为脚本也是用Python编写的。最好找到某种方式来利用它,而不是您正在做的事情。

从Python脚本获取当前目录的父目录

从Python脚本获取当前目录的父目录

我想从Python脚本获取当前目录的父目录。例如,我从/home/kristina/desire- directory/scripts欲望路径启动脚本,在这种情况下是/home/kristina/desire-directory

我知道sys.path[0]sys。但是我不想解析sys.path[0]结果字符串。还有其他方法可以在Python中获取当前目录的父目录吗?

你如何在python中获取当前用户和脚本的工作目录?

你如何在python中获取当前用户和脚本的工作目录?

目前,我使用下面的代码片段静态地为脚本的源数据指定一个文件path:

def get_files(): global thedir thedir = 'C:\Users\username\Documents' list = os.listdir(thedir) for i in list: if i.endswith('.txt'): print("nn"+i) eat_file(thedir+'\'+i)

我静态分配位置的原因是脚本在debugging环境(如Eclipse和Visual Studio Code)中执行时未能正确执行。 这些debugging器假定脚本正在从其工作目录运行。

由于我无法修改可能运行此脚本的每个系统的本地设置,是否有推荐的模块来强制脚本以实用方式收集活动用户(linux和windows)和/或脚本的本地目录?

从PHP exec函数运行GruntJs任务

在MingW上更新G ++会得到大量的错误信息

C#窗体工作站被locking时,Microsoft窗口不会最小化

如果系统处于活动状态,如何检查C ++?

Docker Machine无法使用hyperv成功地到达IP地址/机器

所有Windows计算机具有的且不能更改的ID或序列号

python:为什么os.makedirs会导致WindowsError?

是否有可能在Python中访问GetLongPathName()Win32 API?

在Windows上gtk2hs无法运行

用c ++在Windows中配对蓝牙设备

新的ish pathlib模块 (在Python> = 3.4中可用)非常适合处理类路径对象(包括Windows和其他操作系统)。 用它。 不要打扰过时的os模块。 而且不要试图用裸体的字符串表示类似路径的对象。

这是路径 – 路径一路下降

为了简化:可以构建与对象相同的任何路径(目录和文件路径对象完全相同),该对象可以是绝对路径对象或相对路径对象 。

简单地显示一些有用的路径,比如当前的工作目录和用户主目录,如下所示:

from pathlib import Path # Current directory (relative): cwd = Path() # or Path('.') print(cwd) # Current directory (absolute): cwd = Path.cwd() print(cwd) # User home directory: home = Path.home() print(home) # Something inside the current directory file_path = Path('some_file.txt') # relative path; or file_path = Path()/'some_file.txt' # also relative path file_path = Path().resolve()/Path('some_file.txt') # absolute path print(file_path)

要浏览文件树,你可以做这样的事情。 注意,第一个对象home是一个Path ,其余的只是字符串:

file_path = home/'Documents'/'project'/'data.txt' # or file_path = home.join('Documents','project','data.txt')

要读取位于路径上的文件,可以使用open方法而不是open函数:

with file_path.open() as f: dostuff(f)

但是,你也可以直接抓住文本!

contents = file_path.read_text() content_lines = contents.split('n')

…并直接写文本!

data = 'n'.join(content_lines) file_path.write_text(data) # overwrites existing file

通过这种方式检查它是否是一个文件或目录(并存在):

file_path.is_dir() # False file_path.is_file() # True

创建一个新的空文件,而不用像这样打开它(静默地替换任何现有的文件):

file_path.touch()

只有在文件不存在的情况下才可以使用该文件,使用exist_ok=False :

try: file_path.touch(exist_ok=False) except FileExistsError: # file exists

建立一个新的目录(在当前目录下, Path() )如下:

Path().mkdir('new/dir') # get errors if Path()/`new` doesn't exist Path().mkdir('new/dir',parents=True) # will make Path()/`new` if it doesn't exist Path().mkdir('new/dir',exist_ok=True) # errors ignored if `dir` already exists

以这种方式获取路径的文件扩展名或文件名:

file_path.suffix # empty string if no extension file_path.stem # note: works on directories too

为路径的整个最后部分使用name (词干和扩展名,如果他们在那里):

file_path.name # note: works on directories too

使用with_name方法重命名一个文件(它返回相同的路径对象,但是有一个新的文件名):

new_path = file_path.with_name('data.txt')

你可以使用iterdir遍历目录中的所有“东西”:

all_the_things = list(Path().iterdir()) # returns a list of Path objects

我们今天的关于在Python脚本中获取当前的git hashpython获取git提交当前版本的分享已经告一段落,感谢您的关注,如果您想了解更多关于os.system()是在Python脚本中获取一组文件的最佳方法吗?、从python脚本中获取python脚本的输出、从Python脚本获取当前目录的父目录、你如何在python中获取当前用户和脚本的工作目录?的相关信息,请在本站查询。

本文标签: