How would I be able to create a '.txt' file and store some information from an entry box and be able to update the '.txt' file?
I understand that I would have to use:
file = filedialog.asksaveasfile( mode = 'w', defaultextension = '.txt')
and store the information form the Entrybox into the file:
#the self.nameEntry had the input of 'zack'
name = self.nameEntry.get()
file.write(name)
file.close()
But when the program continues to run and I want to save the new information into the same .txt file, how would I be able to accomplish that without using filedialog.asksaveasfile() all over again? Would I use file = open(file) and then use the file.write()?
I was able to understand how to do it...
Code:
file_name = filedialog.asksaveasfilename(defaultextension = '.txt')
if file_name is None:
return
file = open(file_name, mode 'w')
name = self.nameEntry.get()
file.write(name)
file.close()
Thanks a lot to Kevin for helping me out with my problem :)
Related
I am currently working on encrypting and decrypting files using python and I got to this part. I should replace original file with encryption dictionary and I tried to do so, but when I read the file and write it, it shows original text and encrypt version of it.
My original file contains the text, ABCDEF, and what I got for the result is ABCDEFGH!##$%^&*.
So I am not sure how to get rid of the original text.
Here is my code
import os
Dictionary = {'A':'!','B':'#','C':'#','D':'$','E':'%','F':'^'}
letterfile = input("What's the name of the file?")
exist = os.path.exists(letterfile)
if exist:
letterfile = open(letterfile, "r+")
readfile = letterfile.read()
for i in readfile:
if i in Dictionary:
letterfile.write(Dictionary[i])
letterfile.close()
You don't rewind to the beginning of the file when you write, so you're writing where the reading finished, which is the end of the file.
The right way to do this is to reopen the file for writing. That will empty the file and replace it with the encrypted version.
with open(letterfile, "r") as infile:
readfile = infile.read()
with open(letterfile, "w") as outfile:
for i in readfile:
letterfile.write(Dictionary.get(i, ""))
one way you can accomplish this by putting letterfile.truncate(0) after
readfile = letterfile.read() and before for i in readfile:
fully it would look like this
import os
Dictionary = {'A':'!','B':'#','C':'#','D':'$','E':'%','F':'^'}
letterfile = input("What's the name of the file?")
exist = os.path.exists(letterfile)
if exist:
letterfile = open(letterfile, "r+")
readfile = letterfile.read()
letterfile.truncate(0)
for i in readfile:
if i in Dictionary:
letterfile.write(Dictionary[i])
letterfile.close()
I am writing a Python program where I need to write to a file. I need an if condition to determine if I need to keep writing to same file or open a new file. How do I declare the file so that I can access it with both the if and else? Right now I'm making a test file before the loop just so I have access to the variable. How to avoid opening a TEST.txt file while still having a variable f that I can operate on?
f = open(outputFolder + "TEST.txt", 'w') # how to avoid opening TEST.txt here
while row:
#print(str(row[0]) + '|' + str(row[4]))
currentFileName = getFileName(str(row[0]))
# If coming up on new date open new file
if currentFileName != fileName:
f.close()
fileName = currentFileName
print("Processing: " + fileName)
f = open(outputFolder + fileName, 'w')
f.write(getLine(row))
# else write to current file
else:
f.write(getLine(row))
row = cursor.fetchone()
You didn't work out your logic before writing the program. What you describe in words does not match what you wrote. Start with the words, draw a flowchart, and then write your code.
In your posted program, you're trying to open currentFile multiple times, you don't initialize or change row, and it's not clear what you intend the program to do.
if [condition]:
filename = currentFileName
else:
filename = "TEST.txt"
f = open(filename)
for ... # whatever you're trying to do with your input and output,
# look up the proper method in a file-handling tutorial.
I was wondering if there is anyway that from a filename provided by an user we can get the body of the file
Here as sample of my code
fname = input('Enter filename...')
fileObject = open(fname, 'r')
print(fname)
Yes, you need to read the data from the file and then print it out, or do everything at once:
fname = input("What's the file name? ")
# this assumes it's on the same directory
with open(fname, 'r') as file:
print(file.read())
You can try this: (read the official Python doc. too)
with open(filename) as f:
content = f.read()
# you may also want to remove whitespace characters like `\n` at the end of each line
print(content)
I have a bit of code which prints what I want to save but I cant save it as a variable because of the format. Please can you show me how to save this as a variable so that I can save it into a file
It wont let me add a picture but this is what I want to add to a variable (What its printing)
print(text[i],end="")
x = text[i]
with open("output.txt", 'w') as f:
f.write(x)
or
with open("output.txt", 'w') as f:
f.write(text[i])
Open a file:
f = open('filename','w')
Write a line of text to the file:
f.write(text[i])
And finally close the file:
f.close()
I have a problem that I can't access a file with python even though the file is there and I can access it manually.
The following code is the problem:
f = '~/backup/backup_20121216.log'
text = open(f, "rb").read()
print text
Someone can point me into the right direction?
Does this work?
import os
path = '~/backup/backup_20121216.log'
path = os.path.expanduser(path)
with open(path, 'rb') as fp:
text = fp.read()
print text