How to update global variable in python

In python, I have a function that returns a list of recent links (to folders) on a website. I also have another function that downloads the latest files from these folders. I plan to run this script every day. I have a global list with links to folders that the download function accesses every time it is executed for the last folders. I want to update this global list every five days and keep it for the next 5 days. I run the code until it is updated.

This is something 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 this) and the download function to work every day. So, how can I save the list returned from update () static as a global list until it is updated again?

EDIT: Let me clarify:

I run this on 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, the list was updated and used in download ().

I run this on 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 link3 has not been listed since Monday. How to keep link3 link in the list for the next 5 days until I refresh the list again?

thanks

+4
source share
3 answers

Use the global operator. But there is no need for global for mutable objects if you modify them in place.

You can use modules like pickle to save your list in a file. You can download the list when you want to use it, and save it after making changes.

 lis = ["link1", "link2",...] def update(): global lis #do something return lis 

Peak 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 

You can also use the cPickle module to improve performance .

Additional examples

+8
source

A normal variable declaration will make it local. Use a global keyword to make it renderable as global.

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

If you don't want to run the code yourself, you can use cron-job to do this 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') 
+2
source

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

 list = ["link1", "link2",...] def update(): list.append("link25") return list 

add link 25 to the global list as you like

If you want your list to be constant between runs, you can save it to a file and load it from this file every time or save it to the database and load from the database if you need it to work on several machines

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

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

a source

+1
source

Source: https://habr.com/ru/post/1488173/


All Articles