美文网首页
python 踩坑记录

python 踩坑记录

作者: 嫌疑人螺某 | 来源:发表于2020-01-04 13:43 被阅读0次

最近有个需求是对图片进行批量压缩存储在临时目录中,然后批量上传到oss上

# 遍历目录 将图片压缩到临时目录
for root, dir, files in os.walk(dirpath):
    for file in files:
        cmd = "convert -quality 50 %s %s" % (os.path.join(root, file), os.path.join(tmp_dir, file))
        os.popen(cmd)

# 遍历临时目录 批量上传操作
for file in os.listdir(tmp_dir):
    print(file)

打印dir的值发现,每次打印的文件数量不一致,找到半天没发现原因,最后猜测会不会是上面压缩图片的shell命令还没执行完毕,就继续执行后面的遍历操作导致这个问题,于是加了个延时操作time.sleep(1),发现代码正常了

可以看到os.popen执行shell时是没有阻塞的,可以考虑手动加阻塞或者使用

subprocess.call(cmd,shell=True)

# subprocess.call() 函数说明
def call(*popenargs, timeout=None, **kwargs):
    """Run command with arguments.  Wait for command to complete or
    timeout, then return the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example:

    retcode = call(["ls", "-l"])
    """
    with Popen(*popenargs, **kwargs) as p:
        try:
            return p.wait(timeout=timeout)
        except:
            p.kill()
            p.wait()
            raise

相关文章

  • Pyppeteer踩坑记录

    Pyppeteer踩坑记录 [toc] 简介   pyperteer是puppeteer的Python实现,相比于...

  • Python踩坑记录

    发现上一篇Python记录写到后面像是我的学习笔记了,还是单开一个记录一下踩坑吧。 1.字典python3中,使用...

  • python 踩坑记录

    最近有个需求是对图片进行批量压缩存储在临时目录中,然后批量上传到oss上 打印dir的值发现,每次打印的文件数量不...

  • Selenium 之 Mac 环境下 Python 安装 se

    作为一个Python初级菜鸟 ,以下是我Mac 环境下使用Python 安装selenium 的踩坑记录。 pip...

  • Python读取大文件的"坑“与内存占用检测

    python读写文件的api都很简单,一不留神就容易踩”坑“。笔者记录一次踩坑历程,并且给了一些总结,希望到大家在...

  • python开发踩坑记录

    1 brine是计算机视觉数据的数据集管理器。可以使用Brine轻松地在Python中安装和加载图像数据集。Bri...

  • 2020-10-19随笔 踩坑0传值

    踩坑:当值传入0时,if条件判断时候会自己转换,记录踩坑。

  • Weex入门踩坑记录

    Weex入门踩坑记录

  • Python笔记

    最近在学习python,记点坑,免得以后又踩~ 哦,对了,我用的mac,所以这里只做mac坑的记录,win...

  • ubuntu14.4+python3.6+pip3+pytorc

    ubuntu14.4+python3.6+pip3+pytorch-0.4.1 踩了无数次坑所以记录下过程 1.p...

网友评论

      本文标题:python 踩坑记录

      本文链接:https://www.haomeiwen.com/subject/nwyxactx.html