How to pass python variable to shell command [duplicate]

2024/10/5 16:31:46

Is their any way that i can pass variable value of python to my unix command

Note : var is a python variable which stores filename with path

var='/d/demo/f/f.txt'

Want to call var to my sed command

os.system(bash -c 'sed -i "1i\NAME" var ')

This throws me error at "1i\" syntax invalid

Answer

Use subprocess.run, not os.system(), and pass an array of individual argument-vector entries.

var = '/d/demo/f/f.txt'
subprocess.run(['sed', '-i', r'1i\NAME', var])

Each argument needs to be escaped according to Python rules, not shell rules; hence using r'1i\NAME' for a string with a literal backslash in it. You could also achieve the same effect with '1i\\NAME'.

https://en.xdnf.cn/q/119055.html

Related Q&A

A function inside a function Python

This is a problem asked before but I cant really understand the other explain of this kind of problem so Im here to re-write it in more details. While studying I have encountered this kind of code tha…

Accessing a parameter passed to one function in another function

I have two functions:def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something elsep1 and p2 are the same data, so I want to avoid asking …

SOLVE: AttributeError: ImmutableDenseNDimArray object has no attribute as_independent

I am quite new to python and I have been trying to plot this in a few different ways. If I try using np.vectorize, it crashes. so i wrote this code, which is giving me the error in the title: import ma…

Python memory limit [duplicate]

This question already has answers here:In-memory size of a Python structure(7 answers)Closed 10 years ago.So clearly there cannot be unlimited memory in Python. I am writing a script that creates lists…

Automating Gmail login in Python

I am writing a Python program that can login Gmail.The purpose of this program is to check whether the username/password combination exists and is correct.Since this program is to test the the username…

Reversing order in incrementing digits

I have a list of numbers, and Im trying to do the following in a way as efficient as possible.For each consecutively incrementing chunk in the list I have to reverse its order.This is my attempt so far…

why python selenium get the empty page_source?

I try to simulate the buy item operation on the link below. (Need login in first)taobao_item_linkAnd after you click the button below. img_link:The link will jump to a new link.But if I print out the p…

How do i print the repetition output using regex it prints only first match

I have task where I need to print the occurrence and count and non-occurrence and count import resequence = 1222311m = re.search(r(\d)\1+,sequence)print(m) Exptected output : (1, 1) (3, 2) (1, 3) (2…

How do I get rid of all roles of the user discord.py

I was making my mute command and I thought of getting rid of all the members role but I dont know how to get rid of all the members roles I even tried for role in member.roles:await member.remove_roles…

How to pick the rows which contains all the keywords? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…