python-2.7 - 如何将 Powershell 函数转换为在 Python 脚本中工作?
问题描述
我在 Powershell 中有一个函数,它给出了一个文件夹的路径,返回一个计算的哈希值。我需要它在 Python 脚本中工作。我的问题是:如何将其转换为在 Python 中工作?谢谢。
function Get-FolderHash ($folder) {
dir $folder -Recurse | ?{!$_.psiscontainer} | %{[Byte[]]$contents += [System.IO.File]::ReadAllBytes($_.fullname)}
$hasher = [System.Security.Cryptography.SHA1]::Create()
$a = [string]::Join("",$($hasher.ComputeHash($contents) | %{"{0:x2}" -f $_}))
Write-Host $a
}
Get-FolderHash "PATH_TO_FOLDER"
解决方案
一个简单的选项可以让 Python 执行 Powershell 并捕获 std_out:
导入子进程,系统
p = subprocess.Popen(["powershell.exe", "C:\Users\USER\Desktop\helloworld.ps1"], stdout=sys.stdout)
p.communicate()
但也许你也可以尝试拿出 Powershell 让 Python 处理一切
推荐阅读
- python - 我的动画图像没有使用 matplotlib imshow 动画改变
- javascript - 如何将 404 错误 XMLHttpRequest 修复到本地主机
- google-app-engine - GAE/Go 项目的测试代码放在哪里?
- r - 如何匹配 ID 号以合并两个数据框
- linux - 查找总大小的所有文件模式
- c# - 无法使用数据绑定 DataGridView 设置 Row.Readonly=false
- c# - 填充 ViewModel
- c# - 从具有正确像素格式的流中创建图像
- docker - Docker:在企业防火墙内构建,在外部部署?
- google-dfp - 为什么 DFP/GAM setTargeting 选项不适用于预出价?