4

I see here that I can call a python script from a batch file.

What I want to do is calling a python line without using a script.

This is an example of what i'm trying to do:

>> echo 'python.exe ''' "this/is/a/path".replace('/','\\')'''  '

would return "this\\is\\a\\path"

Note: there is an easier way of doing what i'm trying to do in my example like here but this is not the question :)

Guillaume D
  • 2,202
  • 2
  • 10
  • 37
  • to run python in batch don't use `echo` but `python -c "command"` and you could use `print()` to display it. – furas Nov 21 '19 at 10:47
  • C:\Python27>python.exe -c "print("helloworld")" Traceback (most recent call last): File "", line 1, in NameError: name 'helloworld' is not defined – Guillaume D Nov 21 '19 at 10:49
  • 2
    `-c` option is for passing script as a string. Also remember to use one set of quotes for python inside and the other for the outside - otherwise bash will have problems matching them. For me it's just `python3 -c 'print("this/is/a/path".replace("/","\\"))'` in the shell. – h4z3 Nov 21 '19 at 10:50
  • 2
    You can't use `" "` inside `" "` - you have to use `' '` - ie. `"print( 'helloworld' )"` or `'print( "helloworld" )'` – furas Nov 21 '19 at 10:50

3 Answers3

2

You can use the -c flag:

Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).

python -c '''print("this/is/a/path".replace("/","\\"))'''
> this\is\a\path
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • ```python -c '''print("this/is/a/path".replace("/","\\"))'''``` ... nothing happens – Guillaume D Nov 21 '19 at 10:51
  • It works fine for me using both `python3` and `python2.7`. You don't have to run in in python console. Just run it in your terminal – Giorgos Myrianthous Nov 21 '19 at 10:53
  • 1
    @GuillaumeD this command is not called in python console but in system console/terminal or in script/batch. Don't you get any error message? Maybe you have to add `.exe` or `/full/path/to/python.exe` – furas Nov 21 '19 at 10:54
  • 1
    that will open an interactive python cnsole, not keep him in bash – E.Serra Nov 21 '19 at 10:58
0

this will work, thanks to @E.Serra approach.

echo print("this/is/a/path".replace("/","\\"))  > tmp_file && python tmp_file && rm tmp_file

-->

C:\Python27>echo print("this/is/a/path".replace("/","\\"))  >tmp_python_File && python tmp_python_File && rm tmp_python_File
this\is\a\path
Guillaume D
  • 2,202
  • 2
  • 10
  • 37
-2

OK, if you are just trying to run your python.exe then:

python python.exe 

If you want to run perl style oneliners then:

echo '''print("this/is/a/path".replace("/","\\"))'''  >tmp_python_File && python tmp_python_File && rm tmp_python_File

Spits:

$> this\is\a\path

E.Serra
  • 1,495
  • 11
  • 14
  • ```C:\Python27>echo '''print("this/is/a/path".replace("/","\\"))''' >python.exe && python python.exe && rm python.exe The process cannot access the file because it is being used by another process.``` – Guillaume D Nov 21 '19 at 10:50
  • This answer overwrites the OP's python executable. I think he was just trying to call it, not create such a file. – Tim Nov 21 '19 at 10:50
  • oh wow why the downvote? ```What I want to do is calling a python line without using a script.``` – E.Serra Nov 21 '19 at 10:55