13

In python, i have a function that returns a list of the latest links(to folders) on a website. I also have another function that downloads the latest files from those folders. I plan to run this script everyday. I have a global list with the folder links that the download function accesses everytime it runs for the latest folders. I want to update that global list every five days and keep it static for the next 5 days i run the code until it updates again.

Its sort of like this:

list = ["link1", "link2",...]

def update():
  #code to update list
  return list

def download(list):
  #code to download from links

So I want the update function to run every 5 days(I know how to do that) and the download function to run everyday. So how can i keep the list returned from update() static as the global list until it is updated again?

EDIT: Let me try to clarify:

I run this on a monday:

list = ["link1", "link2"]

def update():
  #code to update list
  return list #--> list = ["link1", "link2", "link3"]

def download(list):
  #code to download from links

this worked fine, list was updated and used in download().

I run this on a Tuesday:

list = ["link1", "link2"]
#update() won't run today, only runs every 5 days
def update():
  #code to update list
  return list #--> list = ["link1", "link2", "link3"]

def download(list):
  #code to download from links

I restarted my code, but now list doesnt have link3 from monday. How do i keep link3 in the list for the next 5 days until i update list again?

Thanks

Vaibhav Aggarwal
  • 1,381
  • 2
  • 19
  • 30

3 Answers3

19

Use global statement. But there's no need of global for mutable objects, if you're modifying them in-place.

You can use modules like pickle to store your list in a file. You can load the list when you want to use it and store it back after doing your modifications.

lis = ["link1", "link2",...]

def update():
  global lis
  #do something
  return lis

Pickle example:

import pickle
def update():
  lis = pickle.load( open( "lis.pkl", "rb" ) ) # Load the list
  #do something with lis                     #modify it 
  pickle.dump( lis, open( "lis.pkl", "wb" ) )  #save it again

For better performance you can also use the cPickle module.

More examples

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • yes, but this will update list for the time the script is running. The script will close in a few minutes. WHen i run it the next day, i want it to be the same list that the update returned the previous day. i hope i am clear enough. Thanks for your help! – Vaibhav Aggarwal Jun 25 '13 at 21:08
  • 1
    @VaibhavAggarwal I guess you need to use modules like pickle to store your list object, see my updated answer. – Ashwini Chaudhary Jun 25 '13 at 21:13
  • @VaibhavAggarwal I've added a pickle related example. – Ashwini Chaudhary Jun 25 '13 at 21:22
  • @VaibhavAggarwal Go for [cPickle](http://docs.python.org/2/library/pickle.html#module-cPickle) then, it's can be up to 1000 times faster than `pickle`. – Ashwini Chaudhary Jun 25 '13 at 21:37
2

Normal declaration of the variable will make it local.
Use global keyword to make it render as global.

Just write the list to a file and access it read it from there later.

If you don't want to self run the code you can use cron-job to do it for you.

def read_file(filename):
    f = open(filename).read().split()
    lis = []
    for i in f:
            lis.append(i)
    return lis 

def write_file(filename,lis):
        f = open(filename,"w")
        for i in lis:
                f.write(str(i)+'\n')
rocker_raj
  • 159
  • 12
1

As long as it is declared in the main program and not within the scope of the function you should be fine to manipulate your list variable from there (your comment) just fine. If you want initialize it as global to from within the scope of a method you can use the global keyword to broaden the scope to the whole program

list = ["link1", "link2",...]

def update():
  list.append("link25")
  return list

will append link25 to the global list as you wanted

If you want your list to be persistent between runs, you can save it to a file and load it from that file each time or save it to a database and load it from a database if you need it to work on multiple machines

you can write the items in your list to a file by doing this

for item in thelist:
    thefile.write("%s\n" % item)

source

Community
  • 1
  • 1
Stephan
  • 16,509
  • 7
  • 35
  • 61