How to load each file in a folder with pickle ( python )

Viewed 1106

I'm writing a script that loads each file in a folder with pickle! But I don't know how to load each file and then perform an action, finish, and then do it with the next file!

Example of loading cookies with the webdriver:

import pickle
import selenium.webdriver

driver = selenium.webdriver.Firefox()
driver.get("http://www.facebook.com")
cookies = pickle.load(open("cookies.txt", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
driver.get("http://www.facebook.com")
#then perform an action 

There are multiple files cookies1.txt, cookies2.txt, cookies3.txt, ... in a folder but i don't know how to run each file one by one, can someone help me? thank you

1 Answers

First get the names of all the cookies files in your folder:

import os
files = os.listdir(path)

Load all your cookies from the different files:

for f in files:
    cookies = pickle.load(open(f, "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)

Then do your stuff

Related