gpt4 book ai didi

使用 Linux 命令的 Python 脚本不能在 Windows 上运行。有什么办法让它与操作系统无关吗?

转载 作者:太空宇宙 更新时间:2023-11-04 08:59:01 29 4
gpt4 key购买 nike

我有一个关于简单脚本的问题。该脚本的目标是找到子目录中的所有 tgz 文件,并从中提取文件名末尾带有 B4 或 B5 的所有 TIF 文件。之后,它将这些文件移动到指定的子目录(分别为 band4 或 band5)。

它似乎在我的 Ubuntu 12.04 机器上运行良好,但是当我的一个伙伴在他的 Windows 7 机器上执行它时,它就崩溃了。据我了解,脚本调用 linux 命令,Windows 无法正确解释它们(找不到 *.tgz 文件)。我想知道是否有办法让它与操作系统无关,从而达到相同的结果。

import subprocess, shlex, os, sys
cmd1 = "find . -name *.tgz"
cmd2 = "xargs -i pigz -dv {}"
args1 = shlex.split(cmd1)
args2 = shlex.split(cmd2)

p1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

cmd1 = "find . -name *.tar"
cmd2 = "xargs -i tar -xfv {} --wildcards '*B5.TIF' '*B6.TIF' '*B8.TIF' -C %s" % repo
args1 = shlex.split(cmd1)
args2 = shlex.split(cmd2)

p1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

pathname = os.path.dirname(sys.argv[0])
b4 = os.path.abspath(pathname)+'/Band_4'
b5 = os.path.abspath(pathname)+'/Band_5'

os.mkdir(b4)
os.mkdir(b5)

cmd1 = "find . -name *B4.TIF"
cmd2 = "xargs -i mv -if {} Band_4"
args1 = shlex.split(cmd1)
args2 = shlex.split(cmd2)

p1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

cmd1 = "find . -name *B5.TIF"
cmd2 = "xargs -i mv -if {} Band_5"
args1 = shlex.split(cmd1)
args2 = shlex.split(cmd2)

p1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

错误回溯:

Traceback (most recent call last):
File "[PATH]\bands.py", line 36, in <module>
p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE)
File "C:\Python27\ArcGIS10.2\lib\subprocess.py", line 711, in _init_
errread, errwrite)
File "C:\Python27\ArcGIS10.2\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
File not found - *.tgz

最佳答案

应该不会太难,只要您了解脚本的作用以及使用的命令/实用程序的作用即可:


这是一个开始:

import errno
import fnmatch
import os
import re
import shutil
import tarfile


cwd = os.getcwd()
REPO = os.path.join(cwd, "repo")


def find(directory, pattern):
for root, dirnames, filenames in os.walk(directory):
for fn in filenames:
if fnmatch.fnmatch(fn.lower(), pattern.lower()):
yield os.path.join(root, fn)


def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise


def mv(src, dst):
try:
shutil.move(src, dst)
except shutil.Error, e:
print "%s, moving on" % e


def matching_tif_files(members):
pattern = re.compile(r'.*B[4568].tif', re.I)
for tarinfo in members:
if pattern.match(tarinfo.name):
print "Extracting %s" % tarinfo.name
yield tarinfo


targz_files = find(cwd, '*.tgz')

for tgz in targz_files:
print tgz
with tarfile.open(tgz) as tar:
tar.extractall(path=REPO, members=matching_tif_files(tar))


b4 = os.path.join(cwd, 'Band_4')
b5 = os.path.join(cwd, 'Band_5')

mkdir_p(b4)
mkdir_p(b5)

b4_tifs = find(cwd, '*B4.tif')
for tif in b4_tifs:
mv(tif, b4)

b5_tifs = find(cwd, '*B5.tif')
for tif in b5_tifs:
mv(tif, b5)

脚本实际上做了一些与您描述的不同的事情。例如,--wildcards '*B5.TIF' '*B6.TIF' '*B8.TIF*B4.TIF 不匹配。我按照我认为合适的方式改编了那些。它当然还不完美,但它应该可以帮助您入门。

关于使用 Linux 命令的 Python 脚本不能在 Windows 上运行。有什么办法让它与操作系统无关吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317989/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com