簡體   English   中英

找到具有特定擴展類型的文件,然后使用 Python 執行 linux 命令

[英]find a file with specific extension type then execute linux commands using Python

我想使用 Python3 備份一個.tar文件,這個.tar將由另一個程序自動生成。 我要啟動備份腳本,它必須首先檢查*.tar是否存在,然后我將執行其他命令,如cpmv

import os
from os import walk

path = '/path/to/directory'  # the directory path that you want to search in
dir_path, dir_names, file_names = next(walk(path))  # You need file_names
for file_name in filenames:
    if '.tar' in file_name:  # or if os.path.splitext(path)[-1] == '.tar'
        <your backup process codes>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM