-1

I want to redirect o/p of shell commands to file using variable "path" but it is not working

import os, socket, shutil, subprocess
host = os.popen("hostname -s").read().strip()
path = "/root/" + host

if os.path.exists(path):
  print(path, "Already exists")
else:
   os.mkdir("Directory", path , "Created")

os.system("uname -a" > path/'uname')  # I want to redirect o/p of shell commands to file using varibale "path" but it is not working 
os.system("df -hP"> path/'df')
Abhimanyu
  • 55
  • 6
  • Use the [subprocess module](https://docs.python.org/3/library/subprocess.html#subprocess.run) – Harvey May 07 '18 at 14:06
  • What does "it is not working" mean exactly? Are you getting an error? – glibdud May 07 '18 at 14:19
  • subprocess.run("uname -a", shell=True, stdout=subprocess.PIPE) Out[199]: CompletedProcess(args='uname -a', returncode=0, stdout=b'Linux abhithak 4.13.0-38-generic #43~16.04.1-Ubuntu SMP Wed Mar 14 17:48:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux\n') which option to use to save in file using variable ? – Abhimanyu May 07 '18 at 14:19
  • /root/abhithak Already exists --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 9 10 ---> 11 os.system("uname -a" > path/'uname') # I want to redirect o/p of shell commands to file using varibale "path" but it is not working 12 os.system("df -hP"> path/'df') TypeError: unsupported operand type(s) for /: 'str' and 'str' – Abhimanyu May 07 '18 at 14:21
  • Please **read** the error it is giving you: `TypeError: unsupported operand type(s) for /: 'str' and 'str'`. The error is with the `/` operator. It tells you this. Look back until you find it. You see `path/'df'`. They are both `'str'`s. You cannot divide strings. It is telling you what's wrong: that you have written something incorrect and not what you intended. – Harvey May 07 '18 at 14:29

2 Answers2

1

I think the problem is the bare > and / symbols in the os.system command...

Here is a python2.7 example with os.system that does what you want

import os
path="./test_dir"
command_str="uname -a > {}/uname".format(path)
os.system(command_str)
hft
  • 1,245
  • 10
  • 29
0

Here's a very minimal example using subprocess.run. Also, search StackOverflow for "python shell redirect", and you'll get this result right away:

Calling an external command in Python

import subprocess


def run(filename, command):
    with open(filename, 'wb') as stdout_file:
        process = subprocess.run(command, stdout=subprocess.PIPE, shell=True)
        stdout_file.write(process.stdout)
        return process.returncode

run('test_out.txt', 'ls')
Harvey
  • 5,703
  • 1
  • 32
  • 41
  • if we use varibale in run it won't work . I want to save in path like host = os.popen("hostname -s").read().strip() ...: path = "/root/" + host – Abhimanyu May 07 '18 at 14:46
  • In [223]: run(path/'uname', 'uname -a') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 run(path/'uname', 'uname -a') TypeError: unsupported operand type(s) for /: 'str' and 'str' – Abhimanyu May 07 '18 at 14:47
  • Just modify my code to do what you want. Write another version of my `run` method to return the hostname rather than write it to a file. – Harvey May 07 '18 at 14:48