0

I have a file with most of my code in it. I have to use a very long list of items for my code and so I am trying to keep it in a separate file (to keep it cleaner and not get in my way).

1) I need to be able to read the list that is in the separate file (python or txt?)

2) I need to be able to append terms to that list that is in the separate file and have it save so it will see those additions in the list next time I run my code

Is there any way to do this? Thanks!

Anderson P
  • 107
  • 1
  • 11
  • 2
    See the *append* mode `'a'` to [`open`](https://docs.python.org/3/library/functions.html#open). Although, your needs sounds suspiciously like a database. – wim Mar 16 '17 at 23:46
  • @wim I am trying to make a database, I'm trying to compile a lot of links (5000ish) and then in my code check to see if I already have the link. Is there a better way to do this? – Anderson P Mar 16 '17 at 23:49
  • 1
    Look at [**`sqlite3`**](https://docs.python.org/2/library/sqlite3.html) in Python's standard library. – Peter Wood Mar 16 '17 at 23:50

2 Answers2

0
with open(<your file name>, 'a') as open_file:
    open_file.write('...your text or variable to be written here...')

The 'a' flag appends without removing content like the typical 'w' write flag.

JacobIRR
  • 8,545
  • 8
  • 39
  • 68
0

Yes! The best way to do it using pickle Python module. Here is example:

import pickle
fizz = [1, 2, 3]
pickle.dump(fizz, open('fizz_list.p', 'wb'))

Then, when you need to get this list:

fizz = pickle.load(open('fizz_list.p', 'rb'))
finomayato
  • 80
  • 7
  • Thanks! What is 'wb' and is .p the file extension? Should I use .py or .txt? – Anderson P Mar 17 '17 at 00:33
  • `wb` - mode of open file. In this case - Write Binary. `.p` - extension for files where pickle objects stored. Be aware, that you can modify yours list only through `pickle`. If you need to modify it - load it first, then modify and dump to the same file again – finomayato Mar 17 '17 at 00:41
  • I'm getting an error when trying to dump my link, the link looks like a regular browser link with https:// but I think it was expecting something else because I get this: `, line 144, in Start alreadyHave = pickle.load(open('database.p')) TypeError: a bytes-like object is required, not 'str'` any way to fix so I can dump strings? – Anderson P Mar 17 '17 at 00:47
  • Oh, my fault. Forgot to open file in binary reading mode. Sorry. I fixed this in answer already – finomayato Mar 17 '17 at 00:50
  • Alright, one last thing. When I run my program I immediately get this error: `, line 144, in Start alreadyHave = pickle.load(open('database.p', 'rb')) EOFError: Ran out of inpu` I looked it up and it may be because the file is empty. In my program I'm trying to use it like a simple database so I can't load the file until I get to the next part of my code. How can I prevent this? – Anderson P Mar 17 '17 at 00:56
  • Create `if` statement with check of the size of yours file. I think it'll work. Here is how you can do it - http://stackoverflow.com/a/2507871/6741497 – finomayato Mar 17 '17 at 01:02