How to tell Python to save files in this folder?
Question: I am new to Python and have been assigned the task to clean up the files in
Slack. I have to backup the files and save them to the designated folder Z
drive Slack Files and I am using the open syntax below but it is producing the
permission denied error for it. This script has been prepared by my senior to
finish up this job.

    
    
    from slacker import *
    import sys
    import time
    import os
    from datetime import timedelta, datetime
    
    root = 'Z:\Slack_Files'
    
    def main(token, weeks=4):
        slack = Slacker(token)
        total = slack.files.list(count=1).body['paging']['total']
        num_pages = int(total/1000.00 + 1)
        print("{} files to be processed, across {} pages".format(total, num_pages))
        files_to_delete = []
        ids = [] 
        count = 1
        for page in range(num_pages):
            print ("Pulling page number {}".format(page + 1))
            files = slack.files.list(count=1000, page=page+1).body['files']
            for file in files:
                print("Checking file number {}".format(count))
                if file['id'] not in ids:
                    ids.append(file['id'])
                    if datetime.fromtimestamp(file['timestamp']) < datetime.now() - timedelta(weeks=weeks):
                    files_to_delete.append(file)
                    print("File No. {} will be deleted".format(count))
                else:
                    print ("File No. {} will not be deleted".format(count))
            count+=1
    
    
        print("All files checked\nProceeding to delete files")
        print("{} files will be deleted!".format(len(files_to_delete)))
        count = 1
        for file in files_to_delete:
            # print open('Z:\Slack_Files')
            print("Deleting file {} of {} - {}".format(count, len(files_to_delete), file["name"]))
            print(file["name"])
            count+=1
    
        return count-1
    
    
    for fn in os.listdir(r'Z:\Slack_Files'):
         if os.path.isfile(fn):
            open(fn,'r') 
    
    if __name__ == "__main__":
        try:
            token = '****'
        except IndexError:
            print("Usage: python file_deleter.py api_token\nPlease provide a value for the API Token")
            sys.exit(2)
    
        main(token)
    

The error it displays is:

    
    
    Traceback (most recent call last):
      File "C:\Users\Slacker.py", line 55, in <module>
    main(token)
      File "C:\Users\Slacker.py", line 39, in main
    print open('Z:\Slack_Files')
    IOError: [Errno 13] Permission denied: 'Z:\\Slack_Files'
    


Answer: To iterate over files in a particular folder, we can simply use os.listdir()
to traverse a single tree.

    
    
    import os  
    for fn in os.listdir(r'Z:\Slack_Files'):
         if os.path.isfile(fn):
            open(fn,'r')        # mode is r means read mode
    

