简体   繁体   中英

How do Python subshell and Linux shell produce different results?

I have a folder with subfolders containing .pp Puppet manifest files. I found out that I can use puppet strings tool to convert .pp files into json.

This can be easily achieved using Unix terminal, running command puppet strings generate --format json file_name.pp .

I wrote a simple python script to iterate over all the files in the specific directory and its subdirectories and run this command on every file.

Sadly, I am getting only empty puppet strings template without any content when running the script, (I implemented printing out the commands that are called), however using the commands works perfectly fine inside Unix terminal.

Please, what am I doing wrong that the commands work in Unix shell but not inside Python's subshell?

Here is the code:

import os


def generate_json():
    for dirpath, dirnames, filenames in os.walk(
            '/home/odolezal/PycharmProjects/puppet_parser/files/'):
        for filename in [f for f in filenames if f.endswith(".pp")]:
            print(os.path.join(dirpath, filename))
            print('puppet strings generate --format json ' + filename)
            os.system('puppet strings generate --format json ' + str(filename))

if __name__ == "__main__":
    generate_json()

我找到了答案 - 将最后一个函数行更改为os.system('puppet strings generate --format json ' + os.path.join(dirpath, filename))非常有效,因为我忘记了脚本不会更改当前目录时对子目录和其中的文件进行操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM