write me a python script that will compare a wsl directory against a windows directory for differences in files if the windows directory doesn't have all the files that the set wsl directory has then copy files from wsl to windows filling in the missing files.
Assistant
Certainly! Below is a Python script that compares a WSL directory against a Windows directory and copies any missing files from the WSL directory to the Windows directory. The script assumes you have access to both directories from within your environment.
What the script does:
Walks through all files in the WSL directory.
Checks if each file exists in the corresponding Windows directory location.
Copies the file from WSL to Windows if it doesn't exist there.
Notes:
You need to ensure Python can access both the WSL and Windows paths.
The script uses shutil for file copying.
It preserves the directory structure.
This script only copies files that don’t exist in Windows directory, it doesn't update files that differ in content or are newer in WSL.
Python script
python
import os
import shutil
def copy_missing_files(wsl_dir, windows_dir):
"""
Copy files that exist in wsl_dir but not in windows_dir.
:param wsl_dir: Path to WSL directory
:param windows_dir: Path to Windows directory
"""
wsl_dir = os.path.abspath(wsl_dir)
windows_dir = os.path.abspath(windows_dir)
for root, dirs, files in os.walk(wsl_dir):
for file in files:
wsl_file_path = os.path.join(root, file)
# Compute relative path from wsl_dir root
rel_path = os.path.relpath(wsl_file_path, wsl_dir)
windows_file_path = os.path.join(windows_dir, rel_path)
# Check if file exists in windows_dir
if not os.path.exists(windows_file_path):
windows_file_dir = os.path.dirname(windows_file_path)
# Create destination directory if it does not exist
if not os.path.exists(windows_file_dir):
os.makedirs(windows_file_dir)
print(f"Copying missing file: {rel_path}")
shutil.copy2(wsl_file_path, windows_file_path)
if __name__ == "__main__":
# Example usage
# Using WSL path in Windows format, e.g. \\wsl$\Ubuntu\home\user\folder
# Or you can access WSL files from WSL python and copy to mounted Windows folder, e.g. /mnt/c/...
wsl_directory = r'\\wsl$\Ubuntu\home\user\my_wsl_dir' # Change this to your WSL directory
windows_directory = r'C:\Users\YourUsername\my_windows_dir' # Change this to your Windows directory
copy_missing_files(wsl_directory, windows_directory)
print("Done copying missing files.")
How to run:
Replace wsl_directory and windows_directory with your actual paths.
If running the script from Windows Python, use the UNC path to WSL: \\wsl$\{distro_name}\{path}
If running inside WSL Python, you can use /mnt/c/... style paths for your Windows directory.
Run the script.
It will print the missing files it copies.
Let me know if you want to add functionality to compare modification times, contents, or delete extras!