Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass file contents to a python script on windows

I would like to pass the contents of a file along with some other parameters to a python script on windows.

On linux, I can do it like this:

less input.txt | my_script.py  > output.txt

On windows I tried the following but it doesn't seem to work:

more input.txt | python my_script.py  > output.txt

Any idea what I'm doing wrong?

like image 507
Pat Mustard Avatar asked Jul 15 '13 07:07

Pat Mustard


People also ask

How do you pass stdin in python?

Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it.

How do you pass variables in a python script?

How to pass information to a Python script using the sys. argv command by hard-coding the input variables in Jupyter Notebook or through the interactive use of the input() function.

How do you access a file from a python script?

Opening Files in PythonPython has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read r , write w or append a to the file.

How do you pass a file name as an argument in python?

This can be done by passing a comma-separated list of file names as one of the arguments while running the script. FOr example, if you have a script called `myscipt.py' you would run it as: python myscript.py file1,file2,file3.


1 Answers

Use following command. It works both in Linux and Windows.

python my_script.py < input.txt > output.txt
like image 185
falsetru Avatar answered Oct 29 '22 14:10

falsetru