Reading .txt files with Python

After studying the basics of Python in the paiza learning environment, I was confused that standard input was not possible after migrating to Atom, and this is a summary when I investigated how to read data from the outside. This is a compilation for beginners, so please let me know if you make any mistakes.

I referred to the following site. ** note.nkmk.me ** -Get, change (move) current directory with Python -Reading and writing (creating / adding) files with Python ** crukky ** -Building a test environment for competitive programming with atom / python

Get the current directory and connect the paths

pass.py


import os
path = os.getcwd()
print(path)

** os.getcwd () ** returns the absolute path of the current directory where Python is currently running as a string.

Read a .txt format file

If you do not move the .txt file to the folder where the path obtained by os.getcwd () is connected, the file cannot be read properly and an error will occur.

Read the entire file as a string

read.py


path = "file name.txt"

with open(path) as f:
    s = f.read()
    print(s)

It is output in the form of ["character string"].

Read lines the entire file line by line

read2.py


path = "file name.txt"

with open(path) as f:
    l_strip1 = [s.strip() for s in f.readlines()]
    print(l_strip1)

It is output in the form of ["1st line", "2nd line", "3rd line", "4th line", ...]. If you want to divide the character string separated by "" (space) into a list and read the whole as a two-dimensional list as shown in paiza's skill check, add ** split ("") **.

read3.py


path = "file name.txt"

with open(path) as f:
    l_strip2 = [s.strip().split(" ") for s in f.readlines()]
    print(l_strip2)

And it is sufficient. Output in the form of [["1-1", "1-2", "1-3"], ["2-1", "2-2", "2-3"], ...] To.

Call a .py format file in Terminal on Atom

Install the package "** platformio-ide-terminal " from Atom's "File> Settings> Install". Then, a small "+" mark will appear at the bottom left of the screen. Click here to call Terminal on Atom. Write " python (file name) .py " to the right of " PS C: \ Users \ (user name) \ (current directory name)> **" in Terminal and press Enter to Terminal. The .py file is called above.

that's all.

Recommended Posts

Reading .txt files with Python
Reading and writing JSON files with Python
[Easy Python] Reading Excel files with openpyxl
[Easy Python] Reading Excel files with pandas
[Python] Reading CSV files
Reading and writing fits files with Python (memo)
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Sorting image files with Python
Integrate PDF files with Python
Recursively unzip zip files with python
Manipulating EAGLE .brd files with Python
[Python] POST wav files with requests [POST]
Decrypt files encrypted with OpenSSL with Python 3
Reading and writing NetCDF with Python
Handle Excel CSV files with Python
Multi-line size specification reading with python
Read files in parallel with Python
Reading and writing CSV with Python
[Python] Easy reading of serial number image files with OpenCV
[AWS] Using ini files with Lambda [Python]
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
"System trade starting with Python3" reading memo
Twilio with Python
Integrate with Python
Play with 2016-Python
Decrypt files encrypted with openssl from python with openssl
AES256 with python
Tested with Python
python starts with ()
Download files on the web with Python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Convert HEIC files to PNG files with Python
Excel with Python
Microcomputer with Python
Cast with python
[Python] Get the files in a folder with Python
Study from Python Reading and writing Hour9 files
Handle zip files with Japanese filenames in Python 3
Drag and drop local files with Selenium (Python)
Character encoding when dealing with files in Python 3
Download and import files with Splunk external python
Upload files to Google Drive with Lambda (Python)
Convert multiple proto files at once with python
Read wav files with only Python standard packages
Example of reading and writing CSV with Python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Upload files with Django
Scraping with Python (preparation)
Try scraping with Python.