Skip to main content
 首页 » 编程设计

python之子进程间歇性地返回空

2024年11月24日8lidabo

我在 Mac OS 上的 python3 中运行一个子进程,以从保存在本地目录中的大量图像中检索 EXIF 图像数据。

代码间歇性地工作。 Ruffly 每 3 次调用子进程返回字节对象(正如预期的那样),只是它是空的 b''。

故障不特定于任何特定图像文件(更改)。

我已经尝试了两个版本的代码,一个是调用 Popen.wait(..) 的地方(例如查找下面的纬度),另一个是立即调用 .communicate() 的地方(例如查找下面的经度)。

print('.......========.......') 
try: 
    cmdLat = "mdls \"" + imagePath + "\" | grep Latitude | awk '{print $3}'" 
    subprocess = Popen(cmdLat, shell=True, stdout=PIPE) 
    Popen.wait(subprocess) 
    lat = subprocess.communicate()[0] 
    latFloat = float(lat.decode()) 
except Exception as e: 
    print("Failed finding latitude, exception:", e) 
    print("lat value: ", lat) 
 
try: 
    cmdLon = "mdls \"" + imagePath + "\" | grep Longitude | awk '{print $3}'" 
    lon = (Popen(cmdLon, shell=True, stdout=PIPE).communicate()[0]) 
    lonFloat = float(lon.decode()) 
except Exception as e: 
    print("Failed finding longitude, exception:", e) 
    print("lon value: ", lat) 

尝试 1 结果:

.......========....... 
IMG_0149.JPG has been successful 
.......========....... 
IMG_0161.JPG has been successful 
.......========....... 
IMG_0377.JPG has been successful 
.......========....... 
Failed finding latitude, exception: could not convert string to float: 
lat value:  b'' 
Failed finding longitude, exception: could not convert string to float: 
lon value:  b'' 

尝试 2 结果:

.......========....... 
IMG_0149.JPG has been successful 
.......========....... 
IMG_0161.JPG has been successful 
.......========....... 
IMG_0377.JPG has been successful 
.......========....... 
IMG_0007.JPG has been successful 
.......========....... 
Failed finding lattitude, exception: could not convert string to float: 
lat value:  b'' 
Failed finding longitude, exception: could not convert string to float: 
lon value:  b'' 

请您参考如下方法:

可能是您引用文件名的方式导致了麻烦,也可能是 Michael Butscher 建议的时间问题。我尝试自己写了一个解决方案,发现我的大部分图片都没有坐标。

这是我的解决方案,请告诉我它是否适合您。请注意,对于那些没有坐标的图片,lat=="(null)" 和 lon="(null)"。对于那些确实有坐标的,lat 和 lon 将是 float 。

#!/usr/bin/env python3 
import pathlib 
import subprocess 
 
def main(): 
    """ Entry """ 
    for pic_file in pathlib.Path('.').glob('*.jpg'): 
        print('-' * 72) 
        print(pic_file) 
        command = ['mdls', 
                   '-name', 'kMDItemLatitude', 
                   '-name', 'kMDItemLongitude', 
                   str(pic_file)] 
        output = subprocess.check_output(command, encoding='utf-8') 
        # Sample output 
        #    kMDItemLatitude  = (null) 
        #    kMDItemLongitude = (null) 
        # or 
        #    kMDItemLatitude  = 46.75725833333333 
        #    kMDItemLongitude = -71.28605666666667         
        print(output) 
 
        # Parse the output 
        lines = output.splitlines() 
        values = [line.split()[-1] for line in lines] 
        print(values) 
 
        # Convert to float 
        try: 
            lat, lon = [float(value) for value in values] 
        except ValueError: 
            lat, lon = values 
 
        print('Latitude =', lat) 
        print('Logitude =', lon) 
 
 
if __name__ == '__main__': 
    main() 

注意事项

  • 我不使用 grep 和 awk 命令,因为我想自己解析这些值
  • 通常,subprocess 函数的输出将返回原始字节数组,我使用 encoding='utf-8' 将其转换为 Python 3 字符串。<