I have 2 pickled files on the computer and I want to load it to my script. Would this code I wrote work in python?
import sys
import pickle
filename1 = sys.argv(1)
filename2 = sys.argv(2)
def read_file(filename1,filename2):
with open(filename1, 'rb') as file1:
file1=pickle.load()
with open(filename1, 'rb') as file2:
file2=pickle.load()
return file1
return file2
sys.exit()
As mentioned here:
1 # Load the dictionary back from the pickle file.
2 import pickle
3
4 favorite_color = pickle.load( open( "save.p", "rb" ) )
5 # favorite_color is now { "lion": "yellow", "kitty": "red" }
So in your case you can do:
def read_file(filename1,filename2):
with open(filename1, 'rb') as file1:
f1=pickle.load(file1)
with open(filename1, 'rb') as file2:
f2=pickle.load(file2)
return f1,f2
There are a few things that don't quite work here:
Your second with-clause is not inside of your function, neither are your returns
A function can only return once. When python reaches the first return inside a function, it exits that function. But you can return multiple things at once, e.g. with return file1, file2
Your pickle-loading is the other way around. Like this, you redefine your file1 and file2 - variables with whatever your call of pickle.load() (notice that it didn't get any arguments) would give you -- what you want is to pass the file you opened to pickle.load and then save the output of that to a new variable
You never actually called your read_file function
your indentation seems to be off
sys.argv is a list, you can't call it (Round braces, like sys.argv(1)) but you can index it (square brackets sys.argv[1])
import sys
import pickle
def read_file(filename1,filename2):
with open(filename1, 'rb') as file1:
data1 = pickle.load(file1)
with open(filename1, 'rb') as file2:
data2 = pickle.load(file2)
return data1, data2
filename1 = sys.argv[1]
filename2 = sys.argv[2]
your_data = read_file(filename1, filename2)
print(your_data) # Now you use your data, i used print as an example
sys.exit() # You usually only write that if you specifically need to
Further suggestion:
You don't necessarily have to make one function to handle two files, you can also just make one function to handle one file and call it twice.
import pickle
def read_file(filename):
with open(filename, 'rb') as file1:
data = pickle.load(file1)
return data1
filename1 = sys.argv[1]
filename2 = sys.argv[2]
your_data = read_file(filename1), read_file(filename2)
print(your_data)
Related
Combine files as handy as possible
Suppose I have the following markdown files
1.md # contains 'foo'
2.md # contains 'bar'
3.md # 'zoo'
4.md # 'zxc'
They are easy to be merged using command cat
$ cat {1..4}.md > merged_5.md
Nevertheless, Python requires multiple steps to achieve this result.
Create Read and Write Methods
def read(filename):
with open(filename) as file:
content = file.read()
return content
def write(filename, content):
with open(filename, 'w') as file:
file.write(content)
Retrieve the qualified files
import glob
filenames = glob.glob('*.md')
In [17]: filenames
Out[17]: ['1.md', '2.md', '3.md', '4.md']
Read and combine
def combine(filenames):
merged_conent = ""
for filename in filenames:
content = read(filename)
merged_content += content
write('merged.md', merged_content)
Encapsulate data and methods in main module and save as 'combine_files.py'
def main():
filenames = glob.glob('*.md')
combine(filenames)
if __name__ == '__main__':
main()
Run it on command line
python3 combine_files.py
It's not handy as command 'cat'
How to refactor the codes to be as handy as possible?
How about something like?:
with open('merged.md', 'w') as out_f:
for filename in glob.glob('*.md'):
with open(filename) as f:
out_f.write(f.read())
How about just do the easy:
def cat(out, *src):
'''Concatenate files'''
with open(out, 'wb') as f:
data = b'\n'.join(open(i, 'rb').read() for i in src)
f.write(data)
You may now call it with cat('merged.md', glob.glob('*.md')). How's that for handy? Certainly much easier than the source of GNU Coreutils.
I am trying to process several files into a single, merged csv file using python. So far, I have
files = ["file1.txt", "file2.txt", "file3.txt"]
def doSomething(oldfile):
content = []
with open oldfile as file:
content = file.read().splitlines()
file.close()
return content.reverse()
with open("newfile.txt", "w") as file:
w = csv.writer(file, dialect = "excel-tab")
for i in range(0, len(files)):
w. writerows(doSomething(files[i])
file.close()
The new file is being created, but there is nothing in it. I am curious about what is going on.
Thanks!
For starters, list.reverse() reverses the list in place and doesn't return anything so you're essentially returning None from your doSomething() function. You'll actually want to split that into two lines:
content.reverse()
return content
If you want to streamline your code, here's a suggestion:
def doSomething(oldfile):
with open(oldfile, "r") as f:
return reversed(f.read().splitlines())
files = ["file1.txt", "file2.txt", "file3.txt"]
with open("newfile.txt", "wb") as file:
w = csv.writer(file, dialect = "excel-tab")
for current_file in files:
w.writerows(doSomething(current_file))
I think your program crashes for several reasons:
open(..) is a function, so you cannot write:
with open oldfile as file:
a with statement for files is used to enforce closing of a file, so file.close() is actually not necessary.
.reverse() works inplace: it returns None, you can use reversed(..) for that.
You can fix it with:
files = ["file1.txt", "file2.txt", "file3.txt"]
def doSomething(oldfile):
content = []
with open(oldfile,'r') as file:
return list(reversed(file))
with open("newfile.txt", "w") as file:
w = csv.writer(file, dialect = "excel-tab")
for oldfile in files:
w.writerows(doSomething(oldfile))
I also used a for loop over the list, instead of the indices, since that is more "pythonic". Furthermore a file is iterable over its rows. So one can use reversed(file) to obtain the lines of the file in reverse.
So this is my code, I would like to save the value 'test' to the file so that it can be called to be used when the program is reopened.
import pickle
test = 0
def Save():
with open('objs.pickle', 'wb') as f:
pickle.dump(test, f)
def Load():
with open('objs.pickle', 'rb') as f:
test = pickle.load(f)
The problem with this code is that when I reopen the program and run in and then type in Load(), it says that 'test' is still equal to 0. (Missing somehting obvious probably)
And so my question is, how could I fix the problem issued in italics?
The global variable test has nothing to do with test inside the function Load(). Change your function to:
def Load():
with open('objs.pickle', 'rb') as f:
return pickle.load(f)
Now this function returns the value it reads from the pickle file.
Call it like this:
print(Load())
Side note: By convention functions names are all lowercase in Python. So the function name should be actually load().
EDIT
The whole program in a better style:
import pickle
def save(file_name, obj):
with open(file_name, 'wb') as fobj:
pickle.dump(obj, fobj)
def load(file_name):
with open(file_name, 'rb') as fobj:
return pickle.load(fobj)
def main():
test = 0
file_name = 'objs.pickle'
save(file_name, test)
print(load(file_name))
if __name__ == '__main__':
main()
I have to create a save function and a load function that saves a dictionary in the format of:
123;Kalle;
123;Maria;
321;Anna;
321;Olle;
My dictionary is supposed to look like a phonebook, with the key being the name and the value is the phonenumber:
telebook = {"jacob":"8472923777", "nisse":"092563243"}
How can I write a function that saves my phonebook in the format mentioned? It should look like this:
8472923777;jacob;
This is my current code:
def save(lista, telebook):
import pickle
filename = lista[1]
f = open(filename, "w")
pickle.dump(telebook, f)
f.close()
print telebook
def load(lista, telebook):
import pickle
try:
filename = lista[1]
f = open(filename, "r")
telebook_1 = pickle.load( f )
telebook.clear()
telebook.update(telebook_1)
f.close()
print telebook
except:
print "This file doesn't exist"
EDIT:
My save function was easier than I thought, managed to solve it on my own. Not sure how to get the load function to work though.
book = raw_input("telebook> ").lower()
lista = book.split()
def save(lista, telebook):
filename = lista[1]
f = open(filename, "w")
for name, num in telebook.items():
f.write(num+";"+name+";"+"\n")
f.close()
print telebook
My load is the same as before but obviously I can't use that one anymore.
def save(telebok, filepath):
with open(filepath, 'w') as outfile:
for name,num in telebok.items():
outfile.write("{};{};\n".format(num, name))
And to get it back:
import csv
def load(filepath):
with open(filepath) as infile:
telebok = dict((v,k) for v,k,_ in csv.reader(infile, delimiter=';'))
return telebok
I have n files in the location /root as follows
result1.txt
abc
def
result2.txt
abc
def
result3.txt
abc
def
and so on.
I must create a consolidated file called result.txt with all the values concatenated from all result files looping through the n files in a location /root/samplepath.
It may be easier to use cat, as others have suggested. If you must do it with Python, this should work. It finds all of the text files in the directory and appends their contents to the result file.
import glob, os
os.chdir('/root')
with open('result.txt', 'w+') as result_file:
for filename in glob.glob('result*.txt'):
with open(filename) as file:
result_file.write(file.read())
# append a line break if you want to separate them
result_file.write("\n")
That could be an easy way of doing so
Lets says for example that my file script.py is in a folder and along with that script there is a folder called testing, with inside all the text files named like file_0, file_1....
import os
#reads all the files and put everything in data
number_of_files = 0
data =[]
for i in range (number_of_files):
fn = os.path.join(os.path.dirname(__file__), 'testing/file_%d.txt' % i)
f = open(fn, 'r')
for line in f:
data.append(line)
f.close()
#write everything to result.txt
fn = os.path.join(os.path.dirname(__file__), 'result.txt')
f = open(fn, 'w')
for element in data:
f.write(element)
f.close()