Can you convert this Powershell script to Python? Here is the code: $sourceDir =…
- User
Can you convert this Powershell script to Python? Here is the code: ``destinationDir = "C:\Users\Nobody\Desktop\syr_xml"
- Assistant
Certainly! Below is a Python script that mimics the functionality of your provided PowerShell script.
import os import shutil source_dir = r"C:\Users\Nobody\Desktop\newtestamentinsy0000unse_g7r6_jp2" destination_dir = r"C:\Users\Nobody\Desktop\syr_xml" # Create the destination directory if it doesn't exist if not os.path.exists(destination_dir): os.makedirs(destination_dir) # Get all .xml files in the source directory xml_files = [f for f in os.listdir(source_dir) if f.endswith('.xml')] for xml_file in xml_files: # Construct the corresponding .jpg file name jpg_file = os.path.join(source_dir, os.path.splitext(xml_file)[0] + ".jpg") # Check if the .jpg file exists if os.path.exists(jpg_file): # Copy the .xml file to the destination directory shutil.copy2(os.path.join(source_dir, xml_file), destination_dir) # Copy the corresponding .jpg file to the destination directory shutil.copy2(jpg_file, destination_dir)