gpt4 book ai didi

python - 删除子目录中的扩展

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:15 24 4
gpt4 key购买 nike

我需要删除扩展名“.tex”:

./1-aoeeu/1.tex
./2-thst/2.tex
./3-oeu/3.tex
./4-uoueou/4.tex
./5-aaa/5.tex
./6-oeua/6.tex
./7-oue/7.tex

请使用下面的一些工具:

  1. Sed 和查找

  2. ruby

  3. python

我的失败尝试:

$find . -maxdepth 2 -name "*.tex" -ok mv `sed 's@.tex@@g' {}` {} +

最佳答案

执行相同操作的 Python 脚本:

import os.path, shutil

def remove_ext(arg, dirname, fnames):
argfiles = (os.path.join(dirname, f) for f in fnames if f.endswith(arg))
for f in argfiles:
shutil.move(f, f[:-len(arg)])

os.path.walk('/some/path', remove_ext, '.tex')

关于python - 删除子目录中的扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1204617/

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