AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1162442
Accepted
BloodThirst
BloodThirst
Asked: 2019-08-01 08:22:56 +0800 CST2019-08-01 08:22:56 +0800 CST 2019-08-01 08:22:56 +0800 CST

如何编写 bash 脚本以对目录中的所有文件运行相同的命令

  • 772

我想对目录中的所有文件运行此命令。

tesseract /home/kong/Documents/input/248.jpg stdout --psm 1 --oem 1 --dpi 300 tsv >/home/kong/Documents/input/ocr_output/input/248.tsv

输入和输出应具有相同的数字,如248.jpg和248.tsv。我尝试编写一个 python 脚本,它导致了分隔符问题。

有人可以帮我弄这个吗 ?我是 bash 新手。

这是我写的python脚本

comm = shlex.split(command)

out_dir = '/home/kong/Documents/input/ocr_output/input'


for file in tqdm(files):
    base_name = os.path.basename(file)
    number = base_name.split('.')[0]
    out_path = '>' + out_dir + '/' + number + '.tsv'
    comm[1] = file
    comm[-1] = out_path
#     tsv = number + '.tsv'
    with open(out_path, 'w') as f:
        subprocess.run(comm, shell=True, stdout=f)
bash 18.04 tesseract
  • 2 2 个回答
  • 367 Views

2 个回答

  • Voted
  1. Best Answer
    schrodingerscatcuriosity
    2019-08-01T09:16:19+08:002019-08-01T09:16:19+08:00

    尝试这个:

    source_dir=/your/source/dir
    output_dir=/your/output/dir
    
    cd "$source_dir" || exit
    
    for file in *.jpg; do
      tesseract "$file" stdout --psm 1 --oem 1 --dpi 300 tsv > "$output_dir/${file%.jpg}.tsv"
    done
    
    • 4
  2. BeastOfCaerbannog
    2019-08-01T10:09:22+08:002019-08-01T10:09:22+08:00

    作为替代方案,您可以将此脚本与 Python 3.5 或更高版本一起使用。

    import os
    import subprocess as sp
    
    # input directory
    in_dir = '/home/kong/Documents/input/'
    # output directory
    out_dir = '/home/kong/Documents/input/ocr_output/input/'
    
    # list of files in input directory
    files = [f for f in os.listdir(in_dir)
             if os.path.isfile(os.path.join(in_dir, f))]
    
    for file in files:
        # input file
        in_file = os.path.join(in_dir, file)
    
        basename = os.path.splitext(file)[0]
        # output file
        out_file = os.path.join(out_dir, basename + '.tsv')
    
        # run command and save its output to out with utf-8 encoding
        out = sp.run(['tesseract', in_file, 'stdout', '--psm', '1',
                      '--oem', '1', '--dpi', '300', 'tsv'],
                     stdout=sp.PIPE).stdout.decode('utf-8')
    
        # save command output to file
        with open(out_file, 'w') as f:
            f.write(out)
    
    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve