[Python] How to get filenames and paths of files in wp-content/uploads folder?

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have no knowledge of php or Js and i need to have a text file with paths/filenames of all images that are in the wp-content/uploads folder of my Wordpress site so i can create an excel spreadsheet to upoload products with image references of path from this text file. I have some excel/vba knowledge to do it.

    I have found this function https://developer.wordpress.org/reference/functions/list_files/ in developes wordpress. Where can i place this code to make tests?

    I tried also to make it happen with Python that i have also very minimum knowledge with no luck. I expeted to logIn to my wp-admin with python through login.php page and then access wp-content/uploads folder but did not make it.

    import os, import requests
    def list_files(directory):
    for root, _, files in os.walk(directory):
    for filename in files:
    filepath = os.path.join(root, filename)
    print(filepath)

    # Set your WordPress login URL and credentials
    wp_login = 'mySite/login.php'
    wp_admin = 'mySite/wp-admin/'
    username = 'admin'
    password = 'admin'

    # Create a session and set the necessary headers
    with requests.Session() as s:
    headers = {'Cookie': 'wordpress_test_cookie=WP Cookie check'}
    data = {
    'log': "myUserName",
    'pwd': "myPass",
    'wp-submit': 'Log In',
    'redirect_to': wp_admin,
    'testcookie': '1'
    }
    # Log in to WordPress
    s.post(wp_login, headers=headers, data=data)

    # Example usage
    uploads_directory = 'mySite/wp-content/uploads/'
    list_files(uploads_directory)

    Continue reading...

Compartilhe esta Página