Read a Python # .txt file for a super beginner in Python with a working .py

environment windows7 (I want a Mac Book Pro 16inch) Visual Studio Code chrome

This article is written for beginners in programming and Python.

Read a .txt file as a working .py file

When I try to read a text file with the python file I'm working on, I get an error saying No such file or directory: and I can't read the text file. 4 hours to fight ... It came to a solution.

Preface-everything started here

file.py


f = open("trial_data.txt")
print(f.read())
f.close()
#FileNotFoundError: [Errno 2] No such file or directory: 'trial_data'

Gabyen! !! !! is. It's been 4 hours since I knocked it down.

Solution-Happy End

Put the text file in the location of current directory

I noticed that. It is "google in python current directory" on the question site. This one word led to a solution.

file.py


import os

currentdir = os.getcwd()

print(currentdir)
#C:\Users\(username)

So put trial_data in C: \ Users \ (username) and it's solved.

It was an embarrassing skill for PC beginners and programming beginners ...

Fin

Recommended Posts

Read a Python # .txt file for a super beginner in Python with a working .py
Read a file in Python with a relative path from the program
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
Read table data in PDF file with Python
Read line by line from a file with Python
[Introduction for beginners] Working with MySQL in Python
How to drop Google Docs in one folder in a .txt file with python
Specific sample code for working with SQLite3 in Python
Run a Python file with relative import in PyCharm
Working with LibreOffice in Python
Working with sounds in Python
Working with LibreOffice in Python: import
Create a binary file in Python
Read files in parallel with Python
Working with DICOM images in Python
[Python] Read a csv file with a large data size using a generator
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
Read CSV file with python (Download & parse CSV file)
Try working with binary data in Python
Tips for dealing with binaries in Python
Let's read the RINEX file with Python ①
Read the file line by line in Python
Process multiple lists with for in Python
Read the file line by line in Python
Get a token for conoha in python
[Python] Read images with OpenCV (for beginners)
[GPS] Create a kml file in Python
[Automation] Read a Word document with Python
Read a character data file with numpy
[Python] Read the specified line in the file
Read text in images with python OCR
I made a configuration file with Python
[Automation] Read mail (msg file) with Python
Read and format a csv file mixed with comma tabs with Python pandas
A memo organized by renaming the file names in the folder with python
Various ways to read the last line of a csv file in Python
Create a GIF file using Pillow in Python
[Python] Get the files in a folder with Python
Get a ticket for a theme park with python
Create a LINE BOT with Minette for Python
How to create a JSON file in Python
[Python] How to read excel file with pandas
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Try working with Mongo in Python on Mac
Replace the named entity in the read text file with a label (using GiNZA)
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
Searching for an efficient way to write a Dockerfile in Python with poetry
Turn multiple lists with a for statement at the same time in Python
Create a Photoshop format file (.psd) with python
Clogged with pip in python super primer (from: can't read / var / mail / PIL)
Create a MIDI file in Python using pretty_midi
Standard .py file used in Python trials (template)-2020
A memo for creating a python environment by a beginner
I want to write to a file with Python