Python Read File Content [duplicate] - python

This question already has answers here:
Easiest way to read/write a file's content in Python
(7 answers)
Closed 6 years ago.
In Python, how to read a file content only (not including attribute and filename), like using InputStream in Java?
I need a method that works for various file formats
I've tried this
with open(filePath, "rb") as imageFile:
str = base64.b64encode(imageFile.read())
M=str.decode()
print(M)
The problem is, I will get error for any object after that block

The most basic way is like so:
with open("filename.txt") as f:
contents = f.read()
The variable contents will now contain a string of everything in the file. More information is in the Python Documentation (https://docs.python.org/3/tutorial/inputoutput.html).

Related

Python reading dictionary from file [duplicate]

This question already has answers here:
Reading JSON from a file [duplicate]
(7 answers)
Closed 1 year ago.
I am trying to read a dictionary from a JSON file which would look something like this.
{"#C": "C:\\Users\\user\\examplefolder}
I would like to load the dictionary from the file into filepath_dict. I am currently using this method to try this however, it refuses to load anything that is not a string.
import json
with open("filepaths.json", "r") as file:
file.write(json.loads(filepaths_dict))
How can I load the JSON dictionary into a python one?
Use json.load
with open("filepaths.json", "r") as f:
filepaths_dict = json.load(f)
json.loads: Will take a string
json.load: Will take a file
Also be careful about using the word "file" as a variable, you are overriding the built-in file function.

How do i get elements from a file and read them to a list in python [duplicate]

This question already has answers here:
How do you read a file into a list in Python? [duplicate]
(8 answers)
Closed 3 years ago.
What i am trying to do is read from a file, and insert the content from a file into a list in python 3. the file should look like this:
♥A
♣A
♥Q
♠Q
and the result i am expecting when i read from the file is that when i print the specific list is that it shows up like this
['♥A', '♣A', '♥Q', '♠Q']
How would i go about solving this?
And i have tried multiple solutions for this, like using for loops, but i dont understand how to do this
You can use the open function.
f = open("file.txt", "r")
l = list()
for line in f:
l.append(line)
f.close()
print(l)

Which is the proper way to read a file in read mode [duplicate]

This question already has answers here:
Easiest way to read/write a file's content in Python
(7 answers)
Closed 3 years ago.
I am trying to read a file in read mode. I can do that in two ways
1st way
file = open('filename', 'r')
content = file.read()
file.close()
2nd way
content = open('filename', 'r').read()
Both codes provide the expected results. I want to know the best way to read a file in read mode
Best way would be to use a context manager so the file is always closed at the end of the context manager scope:
with open(filename, "r") as f:
content = f.read()

How to write JSON to a file with a generic name? [duplicate]

This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 3 years ago.
I'm writing a script that works with certain vocabulary. As a part of it, I want to store the incoming json data of a word in a file. The following code works only for one word at a time. But it will replace the data.json file every time with the json information of the new word.
with open('data.json', 'w') as outfile:
json.dump(data, outfile)
What I'm trying to achieve is: I would like to store the json of each file separately.
For eg: if the word is "internet", then I want the file to export internet.json and if the word is "persistence," then i want the json to store persistence.json
I tried the below, but it throws a syntax error:.
with open(word||'.json', 'w') as outfile:
json.dump(data, outfile)
I'm new to Python (using Python3) and I'm working on a pet-project. So, it would be a great support if you can help me in achieving this. Thanks in advance.

write function parameter inside file - Python [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 5 years ago.
I have a function which prints messages. then I want to save this messages into a file. But when I use .write(function parameter) it only write last message inside my file
writing_in_log = True
def print_and_log(message):
if write_in_log is True:
logFile = open("log", "w+")
logFile.write(message)
logFile.close()
I suppose you are not using the 'a' parameter when opening the file:
with open('file.txt', 'a') as file:
file.write('function parameter')
You probably open the file for each writing with open(yourfile, 'w') which will erase any content from the file before you write to it. If you want to append to your file, use open(yourfile, 'a').
In case this is not the error we need more information about what you are doing, i.e. the relevant parts of the code.

Categories