I am trying to transfer data received from one function (reading) to another (writing).
Existing data inside file.txt should be transfer into json format and be printed to the console, that data should be taken and be written on the file called pfile.txt, by the second function.
I just can't get them to work together. When running each function separately as commands in plain shell, they work; combined, not so much. What am I missing here?
def reading():
filename = 'file.txt'
with open(filename, 'r') as f:
print(json.loads(f.read()))
reading()
def writing():
with open('pfile.txt', 'w+') as pf:
pf.write(reading() in writing()) <-- this doesn't work
pf.write('hello SO') <-- this does work
writing()
When you refer to a function with a pair of parenthesis, Python will call that function with no arguments and resolve it's return value (if any). This is not bash; functions pass data to each other as variables in memory, not through stdin/stdout.
Your code as written appears to be riddled with infinite loops (functions calling themselves) and likely will crash with "recursion depth exceeded" errors. These can be fixed by not calling functions within themselves (or having cycles of functions that call each other).
There's nothing about your code as written that needs multiple functions. I'd go down to 1 function:
def read_and_write():
filename = 'file.txt'
with open(filename, 'r') as f:
content = json.loads(f.read())
print(content)
with open('pfile.txt', 'w+') as pf:
pf.write(content)
If you want two functions, try the following:
def read():
filename = 'file.txt'
with open(filename, 'r') as f:
content = json.loads(f.read())
return content
def write():
content = read()
with open('pfile.txt', 'w+') as pf:
pf.write(content)
Related
I want to write to a file from a script that also includes functions. Everything seems to work, except that it writes nothing to the file (it writes "" to the file, so it essentially wipes it) if there is a function present in the script; even if it writes before the function is defined.
(Running Python 3.9.0)
This writes "A" to test.txt:
f = open("test.txt", "w")
f.write("A")
f.close
This writes "" to test.txt:
f = open("test.txt", "w")
f.write("A")
f.close
def func():
return 1
What am I doing wrong/how can I work around it?
Recently I came across a strange behavior of the with open() statement in Python.
The following code returns output just for the first read-statement, having an empty lines-list.
input_csv = []
with open(self.path, 'r') as f: # Opening the CSV
r = csv.DictReader(f)
for row in r:
input_csv.append(row) # Storing its contents in a dictionary for later use
lines = f.readlines() # Reading it in as a list too
f.close()
While splitting it into two open () statements returns the objects as desired.
input_csv = []
with open(self.path, 'r') as f: # Opening the CSV
r = csv.DictReader(f)
for row in r:
input_csv.append(row) # Storing its contents in a dictionary for later use
f.close()
with open(self.path, 'r') as f: # Opening the CSV
lines = f.readlines() # Reading it in as a list too
f.close()
Why is the f variable just used once in the first statement?
Many thanks
If you look into documentation of csv.reader() which is used for DictReader().reader:
Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its __next__() method is called...
Hence, it uses behavior of file-like object for which each iteration essentially is f.readline(). An operation which also advances current position in the file... until EOF is reached, which when iteration raises StopIteration exception. It is the same behavior you would observe trying:
with open(self.path, 'r') as f:
for l in f:
pass # each line was read
print(f.readlines())
You can add print(f.tell()) to see how the position changes as you execute each line.
If you (re)open a new file, you start at position 0 (again). If you've read through once and wanted to use the same handle again, you need to return to the beginning of the file: f.seek(0).
Note: you really do not need to perform f.close() in a managed context using with. Once you leave it, it'll close the file handle for you.
def fileCounter():
infile = open('words.txt','r') # Open the source file
outfile = open('File_Results.txt','w')
data = infile.read()
lineCount = len(data.split('\n'))
wordCount = len(data.split())
charCount = len(data)
results = print(lineCount,wordCount,charCount)
infile.close()
outfile.write()
outfile.close()
fileCounter()
I'm new to coding and this is my first time working with files. How do I write results in my outfield. I keep getting this error - TypeError: write() argument must be str, not None
You need to write something. Something goes between the parentheses for outfile.write(). My guess is that you want something like this:
outfile.write("{} {} {}".format(lineCount, wordCount, charCount))
Your result = print(...) doesn't save anything. It prints to your console.
Another approach would be redirecting your prints to your file:
from contextlib import redirect_stdout
def fileCounter():
with (open('words.txt','r') as infile, open('File_Results.txt','w') as outfile):
data = infile.read()
lineCount = len(data.split('\n'))
wordCount = len(data.split())
charCount = len(data)
with redirect_stdout(outfile):
print(lineCount,wordCount,charCount)
fileCounter()
Note that I also used context managers to automatically handle opening and closing files. This approach is safer because it'll close the files (and stop redirecting STDOUT) even if an exception occurs.
There is no argument to your outfile.write() function. There needs to be some content to be written to the file, that needs to be passed as parameter to the function.
For Example:
# To write 'I am new to Python'
outfile.write('I am new to Python')
the argument to the write function must be a string.
if this line
results = print(lineCount,wordCount,charCount)
prints the stuff you want to have in the output file, you might do something like
results = "%s, %s, %s" % (lineCount,wordCount,charCount)
outfile.write(results)
outfile.close()
There are no arguments in outfile.write() so it writes nothing.
I assume you want to write the data of infile in outfile, so you do the following:
outfile.write(lineCount)
outfile.write(wordCount)
outfile.write(charCount)
In outfile.write() you wish to include whatever you're writing to file. In this case, you could do something like:
#put results into a list
results = [lineCount, wordCount, charCount]
#print results
print(results)
#write results to a file
outfile.write(",".join(results))
Two things in your code that are interesting. First, as far as I'm aware, print returns None so results in your current code is None. Second, in the corrected rendition, results is a list but in order to write it to file you need to convert it to a string. We do that by joining the elements in the list, in this case with a comma.
I'm trying to create a simple function which I can use to store json data to a file. I currently have this code
def data_store(key_id, key_info):
try:
with open('data.txt', 'a') as f:
data = json.load(f)
data[key_id] = key_info
json.dump(data, f)
pass
except Exception:
print("Error in data store")
The idea is the load what data is currently within the text file, then create or edit the json data. So running the code...
data_store("foo","bar")
The function will then read what's within the text file, then allow me to append the json data with either replacing what's there (if "foo" exists) or create it if it doesn't exist
This has been throwing errors at me however, Any ideas?
a mode would not work for both reading and writing at the same time. Instead, use r+:
with open('data.txt', 'r+') as f:
data = json.load(f)
data[key_id] = key_info
f.seek(0)
json.dump(data, f)
f.truncate()
seek(0) call here moves the cursor back to the beginning of the file. truncate() helps in situations where the new file contents is less than the old one.
And, as a side note, try to avoid having a bare except clause, or/and log the error and the traceback properly.
Sometimes when I open a file for reading or writing in Python
f = open('workfile', 'r')
or
f = open('workfile', 'w')
I read/write the file, and then at the end I forget to do f.close(). Is there a way to automatically close after all the reading/writing is done, or after the code finishes processing?
with open('file.txt','r') as f:
#file is opened and accessible via f
pass
#file will be closed before here
You could always use the with...as statement
with open('workfile') as f:
"""Do something with file"""
or you could also use a try...finally block
f = open('workfile', 'r')
try:
"""Do something with file"""
finally:
f.close()
Although since you say that you forget to add f.close(), I guess the with...as statement will be the best for you and given it's simplicity, it's hard to see the reason for not using it!
Whatever you do with your file, after you read it in, this is how you should read and write it back:
$ python myscript.py sample.txt sample1.txt
Then the first argument (sample.txt) is our "oldfile" and the second argument (sample1.txt) is our "newfile". You can then do the following code into a file called "myscript.py"
from sys import argv
script_name,oldfile,newfile = argv
content = open(oldfile,"r").read()
# now, you can rearrange your content here
t = open(newfile,"w")
t.write(content)
t.close()