I am new to Python and I was wondering how to save this hashed password list (stored in variable passwords)
['73868cb1848a216984dca1b6b0ee37bc', '2de9210e9173ca4151bb220a2ded6cdb', '8c064f4067cf0c59c68ec281f5786cb2']
to a text file in the format:
73868cb1848a216984dca1b6b0ee37bc
2de9210e9173ca4151bb220a2ded6cdb
8c064f4067cf0c59c68ec281f5786cb2
currently I am able to save it to a text file however it saves as
['73868cb1848a216984dca1b6b0ee37bc', '2de9210e9173ca4151bb220a2ded6cdb', '8c064f4067cf0c59c68ec281f5786cb2']
current code to save file:
f = open( 'hash.txt', 'w' )
f.write(repr(passwords) + '\n' )
f.close()
Please help :) Thank you
Reason for wanting to save in text is so I can call that list in a new script to decrypt them however because it saves as a list (and imports as a list) it creates a list in a list and messes up my decryption script.
Edit: Thank you all for the great answers :) its a huge help!
You can save it like this:
with open('hash.txt', 'w') as f:
f.write('\n'.join(passwords))
This achieves what you waht to do. (You could avoid the with-statement)
Load it like this:
passwords = []
with open('hash.txt', 'r') as f:
passwords.extend(f.read().split('\n'))
To use Python 3, you would have to be careful to convert the bytes to str first...
Greets.
EDIT:
Sorry, it must be passwords.extend() - not passwords.append().
You should use readlines() to pass the text from the file into the list and use that list like so:
passwords = []
with open('hash.txt', 'r') as f:
passwords = f.readlines()
f.close()
If you come across '\n', then just use something like "".join(passwords)
You can use the following code :
f = open( 'hash.txt', 'w' )
for a in passowrds:
f.write(a + '\n' ) #you have to pass them as separate variables
f.close()
Whenever you have to take out take out values from a list, you can use the for loop Example
list = ["cat","dog","lion"]
for animal in list:
print animal
This will print each animal in the list. You can use any other variable name in place of animal.
To open:
f = open('hash.txt', 'w')
To save:
f.write(file_path)
Related
I have some question about my code:
def entry_book():
book = ["autor", "ime", "godina", "ISNB", "zanr", "cena", "broj"]
print ("Podaci za knjigu:")
book[0] = input ("Autor: ")
book[1] = input ("Naslov: ")
book[2] = input ("Godina: ")
book[3] = input ("ISNB: ")
book[4] = input ("Zanr: ")
book[5] = input ("Cena: ")
book[6] = input ("Kolicina: ")
record= "{}|{}|{}|{}|{}|{}|{}".format (book[0], book[1], book[2], book[3],
book[4], book[5], book[6])
print (book)
print (record)
f = open('books.txt','w')
f.write (record)
f.close()
f = open("books.txt")
x = f.read()
f.close()
print (x)
record1 = record.split('|')
print (record1)
second_meni()
This is code to store information on books, which I want to access later (like at a library). However, every time I add/create a new book, the old one gets deleted. Can anyone help me rewrite the code so it can store the old data as well. Or please explain what is the correct way to do it?
You have to use
f = open('knjige.txt', 'a')
'w' recreates the file (so use it only for NEW files, or if you don't mind it will be overwritten, 'a' appends to a file.
See python open built-in function: difference between modes a, a+, w, w+, and r+?
Also some unrelated suggestions:
Use the add instead of indices, or even better: use a dictionary
Use English variable names/comments.
Use code to check if the file read/write is ok, what if the file cannot be
written because of access restrictions or too less space on the disk?
Use different functions for the input, writing and printing, it makes testing/maintainability/extension much easier.
I took the liberty of pythonizing your code a bit.
def unos_knjiga():
headers = ["Autor", "Naslov", "Godina", "ISNB", "Zanr", "Cena", "Kolicina"]
print("Podaci za knjigu:")
knjiga = [input("{}".format(obj + ': ')) for obj in headers] # see 1
zapis = '|'.join(knjiga) # see 2
print(knjiga)
print(zapis)
with open('knjige.txt', 'a') as f: # see 3
f.write(zapis + '\n')
# i guess this is for testing?
with open("knjige.txt", 'r') as f:
x = f.read()
print(x)
# and this too?
zapis1 = zapis.split('|')
print(zapis1)
# this is not mentioned anywhere
second_meni()
1) This is a list comprehension. It creates lists by looping through stuff. In this case we are looping through the header list and use its items to construct input statements. The provided input is stored in the list.
2) .join() method. It does what you explicitly did. Joins items from iterators using a string between them.
3) the with keyword. Manages files so that you do not have to. Unless there is a reason not to use it, use it. This was also where the real problem with your code was. You have to use the 'a' mode. 'a' is for append, 'w' is for write. In this context, write means delete everything that was there and write this new stuff. Also note that 'a' mode can also create files, you do not need to temporarily switch to 'w' for that ('r' does not; 'r' is for read).
Cheers!
I think there are two methods to do this:
FIRST
f = open('knjige.txt','w')
is the piece of code which is responsible for rewriting the existing data in your file.
Other option which python offers to append some new data to the existing data is to open a file for writing using append 'a' method. So you can replace your above statement with
f = open('knjige.txt','a')
It won't replace the file with new data you enter.
SECOND
Other option is to open your file in read method, f = open('knjige.txt','r') and copy the existing data to a variable ( variable=f.read('knjige.txt') ). You can also use pickle module and its functions dump and load if you need to maintain your datatype.
Now concatenate your new data to the values in 'variable' and again open the file in write method and write it back to it.
Your call to open the file, f = open('knjige.txt','w') opens the file, truncating the existing contents should it exist. If you open the file with a mode that appends contents, like a it should not delete previous lines. See https://docs.python.org/2/library/functions.html#open for more information on opening files for reading / writing.
I'm trying to write a simple Phyton script that alway delete the line number 5 in a tex file, and replace with another string always at line 5. I look around but I could't fine a solution, can anyone tell me the correct way to do that? Here what I have so far:
#!/usr/bin/env python3
import od
import sys
import fileimput
f= open('prova.js', 'r')
filedata = f,read()
f.close ()
newdata = "mynewstring"
f = open('prova.js', 'w')
f.write(newdata, 5)
f.close
basically I need to add newdata at line 5.
One possible simple solution to remove/replace 5th line of file. This solution should be fine as long as the file is not too large:
fn = 'prova.js'
newdata = "mynewstring"
with open(fn, 'r') as f:
lines = f.read().split('\n')
#to delete line use "del lines[4]"
#to replace line:
lines[4] = newdata
with open(fn,'w') as f:
f.write('\n'.join(lines))
I will try to point you in the right direction without giving you the answer directly. As you said in your comment you know how to open a file. So after you open a file you might want to split the data by the newlines (hint: .split("\n")). Now you have a list of each line from the file. Now you can use list methods to change the 5th item in the list (hint: change the item at list[4]). Then you can convert the list into a string and put the newlines back (hint: "\n".join(list)). Then write that string to the file which you know how to do. Now, see if you can write the code yourself. Have fun!
This question already has answers here:
Write and read a list from file
(3 answers)
Closed 8 years ago.
I was wondering how I can save a list entered by the user. I was wondering how to save that to a file. When I run the program it says that I have to use a string to write to it. So, is there a way to assign a list to a file, or even better every time the program is run it automatically updates the list on the file? That would be great the file would ideally be a .txt.
stuffToDo = "Stuff To Do.txt"
WRITE = "a"
dayToDaylist = []
show = input("would you like to view the list yes or no")
if show == "yes":
print(dayToDaylist)
add = input("would you like to add anything to the list yes or no")
if add == "yes":
amount=int(input("how much stuff would you like to add"))
for number in range (amount):
stuff=input("what item would you like to add 1 at a time")
dayToDaylist.append(stuff)
remove = input("would you like to remove anything to the list yes or no")
if add == "yes":
amountRemoved=int(input("how much stuff would you like to remove"))
for numberremoved in range (amountRemoved):
stuffremoved=input("what item would you like to add 1 at a time")
dayToDaylist.remove(stuffremoved);
print(dayToDaylist)
file = open(stuffToDo,mode = WRITE)
file.write(dayToDaylist)
file.close()
You can pickle the list:
import pickle
with open(my_file, 'wb') as f:
pickle.dump(dayToDaylist, f)
To load the list from the file:
with open(my_file, 'rb') as f:
dayToDaylist = pickle.load( f)
If you want to check if you have already pickled to file:
import pickle
import os
if os.path.isfile("my_file.txt"): # if file exists we have already pickled a list
with open("my_file.txt", 'rb') as f:
dayToDaylist = pickle.load(f)
else:
dayToDaylist = []
Then at the end of your code pickle the list for the first time or else update:
with open("my_file.txt", 'wb') as f:
pickle.dump(l, f)
If you want to see the contents of the list inside the file:
import ast
import os
if os.path.isfile("my_file.txt"):
with open("my_file.txt", 'r') as f:
dayToDaylist = ast.literal_eval(f.read())
print(dayToDaylist)
with open("my_file.txt", 'w') as f:
f.write(str(l))
for item in list:
file.write(item)
You should check out this post for more info:
Writing a list to a file with Python
Padraic's answer will work, and is a great general solution to the problem of storing the state of a Python object on disk, but in this specific case Pickle is a bit overkill, not to mention the fact that you might want this file to be human-readable.
In that case, you may want to dump it to disk like such (this is from memory, so there may be syntax errors):
with open("list.txt","wt") as file:
for thestring in mylist:
print(thestring, file=file)
This will give you a file with your strings each on a separate line, just like if you printed them to the screen.
The "with" statement just makes sure the file is closed appropriately when you're done with it. The file keyword param to print() just makes the print statement sort of "pretend" that the object you gave it is sys.stdout; this works with a variety of things, such as in this case file handles.
Now, if you want to read it back in, you might do something like this:
with open("list.txt","rt") as file:
#This grabs the entire file as a string
filestr=file.read()
mylist=filestr.split("\n")
That'll give you back your original list. str.split chops up the string it's being called on so that you get a list of sub-strings of the original, splitting it every time it sees the character you pass in as a parameter.
I am saving a dictionary of student names as keys and grades lists as values. I am attempting to write the values to a file. At the moment I am writing them as strings.
def save_records(students, filename):
#saves student records to a file
out_file = open(filename, "w")
for x in students.keys():
out_file.write(x + " " + str(students[x]) + "\n")
out_file.close()
After saving the file, I try to read it back. The pertinent part of the read out is below.
while True:
in_line = in_file.readline()
if not in_line:
break
#deletes line read in
in_line = in_line[:-1]
#initialize grades list
in_line = in_line.split()
name = in_line[0]
students[name] = map(int, in_line[1:])
The read out code works well for normal text files that are pre-formatted. The format of the textfile is: key (whitespace) values separated by whitespace "\n". I would like to know how to write in to a text file by combining string and list elements.
If you have control over writing the data, I would recommend using a well-established format, such as JSON or INI. This would allow you to make use of common libraries, such as the json or ConfigParser modules, respectively.
Would it not be easier to use something like pythons pickle which is for storing things like dicts
...and then pretty print output to a separate file?
It's hard to say without knowing how you plan on using this...
Since students[name] = map(int, in_line[1:]), i assume you want to print the items of the list student[x] with whitespaces inbetween.
You could use the str.join method
' '.join(map(str,students[x]))
You may want to consider using Comma Separated Value files (aka csv files) instead of plain text files, as these provide a more structured way to read and write your data. Once written, you can open them in a spreadsheet program like Excel to view and edit their contents.
Re-writing your functions to work with csv files, and assuming you are using Python 2.x, we get something like:
import csv
def save_records(students, filename):
# note that csv files are binary so on Windows you
# must write in 'wb' mode; also note the use of `with`
# which ensures the file is closed once the block is
# exited.
with open(filename, 'wb') as f:
# create a csv.writer object
csv_out = csv.writer(f)
for name, grades in students.iteritems():
# write a single data row to the file
csv_out.writerow([name]+grades)
def read_records(filename):
students = dict()
# note that we must use 'rb' to read in binary mode
with open(filename, 'rb') as f:
# create a csv.reader object
csv_in = csv.reader(f)
for line in csv_in:
# name will have type `str`
name = line[0]
grades = [int(x) for x in line[1:]]
# update the `students` dictionary
students[name] = grades
return students
I'm getting a bit of a trouble here. I have a text file with ["Data1", "Data2", "Data3"], and I want to make that if data1 is not in the file, then append a new list with all three strings, and if data is already there, then just print it. What is broken in this code and why?
filename = "datosdeusuario.txt"
leyendo = open(filename, 'r')
if user.name in leyendo:
Print("Your user name is already there")
else:
file = open(filename, 'a')
file.write(json.dumps([user.name, "data2", "data3"])+"\n")
file.close()
Print("Since I couldn't find it, I did append your name and data.")
P.S.: I am a rookie in Python, and I'm getting confused often. That's why I am not using any dicts (no idea what they are anyway), so I'd like to make that code work in the most simple way.
P.S.2: Also, if that works, my next step would be to make a search engine to return one specific of the three data items in the list. For example, if I want to get the data2 in a list with username "sael", what would I need to do?
It seems that you're reading from the file pointer, NOT from the data in the file as you expected.
So, you first need to read the data in the file:
buffer = leyendo.read()
Then do your check based on buffer, not leyendo:
if user.name in buffer:
Also, you're opening the file two times, that may be kind of expensive. I am not sure if Python got a feature to open the file in both read and write modes.
Assuming that your user.name and your Print functions are working, you need to read the file and close the file.
Try this:
filename = "datosdeusuario.txt"
f = open(filename, 'r')
leyendo = f.read()
f.close()
if user.name in leyendo:
Print("Your user name is already there")
else:
file = open(filename, 'a')
file.write(json.dumps([user.name, "data2", "data3"])+"\n")
file.close()
Print("Since I couldn't find it, I did append your name and data.")
First, you should close the file in both cases, and I think you should close the file before re-opening it for appending.
I think the problem is with the line:
if user.name in leyendo:
which will always return false.
You should read the file and then question it like so:
if user.name in leyendo.read():