[Python] Change standard input from keyboard to text file

I'm not sure how to write it in Qiita's first post, but I will write down the convenient usage of python little by little.

    1. How to change standard input to a text file instead of a keyboard -When repeatedly calculating the same type of data while changing the conditions, it is difficult to input each time from the keyboard. So I searched for a way to enter the condition as a text file. ・ I tried several things based on the internet information and arrived at the simplest method.

import sys #Because sys.stdin is required when switching standard input to a file fn = input () #Enter text file name / If you keep the keyboard,'-' if not fn =='-': #'-' is the keyboard sys.stdin = open (fn) # By setting fn to stdin, input will be executed from file fn thereafter. ... # Describe the process sys.stdin.close () #close standard input file

I'm a beginner. Thank you for your supervision.

Recommended Posts

[Python] Change standard input from keyboard to text file
Python: Use zipfile to unzip from standard input
Did not change from Python 2 to 3
Change the standard output destination to a file in Python
How to read text by standard input or file name specification like cat in Python
[Python] Standard input
Python3 standard input I tried to summarize
[Python] Add comments to standard input files
Import Excel file from Python (register to DB)
From file to graph drawing in Python. Elementary elementary
Wav file generation from numeric text with python
Changes from Python 2 to Python 3.0
[Python] About standard input
python> Change file owner> os.chown ("path / to / file", uid, gid)
Have python parse the json entered from the standard input
Procedure to exe python file from Ubunts environment construction
[Work efficiency] How to change file names in Python
Python script to create a JSON file from a CSV file
Cheating from PHP to Python
Convert HTML to text file
[Python3] Standard input [Cheat sheet]
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Receiving standard input tips @ python
Switch from python2.7 to python3.6 (centos7)
Speech to speech in python [text to speech]
Connect to sqlite from python
How to change Python version
[Python] How to use input ()
[python] Change the image file name to a serial number
Allow Python to select strings in input files from folders
[Python] How to change EXCEL file saved in xlsb to xlsx
(Translation) Native connection from Python to Hadoop file system (HDFS)
Convert Excel file to text in Python for diff purposes
Call Matlab from Python to optimize
Execute Python script from batch file
Matrix representation with Python standard input
Tips on Python file input / output
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Post from python to facebook timeline
Output to csv file with Python
[Lambda] [Python] Post to Twitter from Lambda!
Change python default encoding to utf-8
Extract text from images in Python
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
Notes for Python file input / output
Useful to remember! 10 Python Standard Libraries
How to access wikipedia from python
Python to switch from another language
Change the active version in Pyenv from anaconda to plain Python
File upload to Azure Storage (Python)
[Python] Change the alphabet to numbers
Update Python on Mac from 2 to 3
Extract lines that match the conditions from a text file with python
Change AWS EC2 instance from t2 to t3
Python Paiza-Various skill checks and standard input
[Python] How to convert db file to csv
[Python] Fluid simulation: From linear to non-linear
Change Python 64bit environment to 32bit environment with Anaconda