Python Forum
Running script from remote to server
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running script from remote to server
#1
I had a request to help with the process of transferring scanned records from one folder to another and creating subfolders for new records. I was able to get the code working locally on my computer, but it wouldn't work when I sent it to the end user. Would this work running from a client to the server, or does it need to be run on the server? The source and destination were updated to Z drive info for their server.

import os
import shutil
from datetime import datetime
import tkinter as tk
from tkinter import messagebox

# Source and destination directories
source_dir = r'C:\Users\justi\OneDrive\Desktop\scanned'
destination_dir = r'F:\scanned'

# Function to transfer documents and generate a report
def transfer_documents():
    # Variables to track the number of files and folders
    files_transferred = 0
    new_folders_created = 0

    # Loop through all files in the source directory
    for filename in os.listdir(source_dir):
        # Skip directories and process all files regardless of extension
        if os.path.isfile(os.path.join(source_dir, filename)):
            # Split the filename to get first name, last name, and date of birth
            name_parts = filename
            name_without_extension, _ = os.path.splitext(name_parts)  # Remove file extension
            parts = name_without_extension.split(',')

            if len(parts) >= 3:  # We expect at least three parts: first name, last name, and date of birth
                first_name = parts[0].strip()
                last_name = parts[1].strip()
                dob = parts[2].strip()

                # Verify that the date of birth is in a valid format (MM-DD-YYYY)
                try:
                    # Check if the date format is valid
                    datetime.strptime(dob, "%m-%d-%Y")
                    # Construct the folder name based on first name, last name, and date of birth
                    folder_name = f"{first_name}, {last_name}, {dob}"
                    destination_folder = os.path.join(destination_dir, folder_name)

                    # Create the folder in the destination directory if it doesn't exist
                    if not os.path.exists(destination_folder):
                        os.makedirs(destination_folder)
                        new_folders_created += 1

                    # Construct the full path to the file
                    source_file_path = os.path.join(source_dir, filename)
                    destination_file_path = os.path.join(destination_folder, filename)

                    # Move the file to the new folder
                    shutil.move(source_file_path, destination_file_path)
                    files_transferred += 1
                except ValueError:
                    print(f"Skipping {filename}: Invalid date format for date of birth.")

    # Create a completion report inside the destination directory
    report_file_path = os.path.join(destination_dir, 'transfer_report.txt')
    with open(report_file_path, 'w') as report:
        report.write(f"Transfer Report - {datetime.now()}\n")
        report.write("="*50 + "\n")
        report.write(f"Total files transferred: {files_transferred}\n")
        report.write(f"New folders created: {new_folders_created}\n")
        report.write("="*50 + "\n")

    # Popup completion message
    root = tk.Tk()
    root.withdraw()  # Hide the main window
    messagebox.showinfo(
        "Transfer Complete",
        f"Transfer complete. {files_transferred} files were transferred.\n"
        f"{new_folders_created} new folders were created.\n"
        f"Completion report generated at: {report_file_path}"
    )
    root.destroy()

# Run the function
if __name__ == '__main__':
    transfer_documents()
Reply
#2
Questions:
  1. What error is end user getting? (ask for complete unaltered traceback, and post here)
  2. You have hard coded paths in your code, Is the directory structure the same on end users computer?
Gribouillis likes this post
Reply
#3
(Mar-27-2025, 02:03 PM)Larz60+ Wrote: Questions:
  1. What error is end user getting? (ask for complete unaltered traceback, and post here)
  2. You have hard coded paths in your code, Is the directory structure the same on end users computer?

The source and destination were updated on the end user's side. When they run the script, they just get a temporary window popup.
I have never run a script from a client to a server, so I'm unsure if there is anything different I should have done or if it would be easier if they ran this script from the server itself. I am only able to do support over the phone with them and walked them thru the best I could to replace the pathways. Example of the path they put in (source_dir = r'Z:\scanned').
Reply
#4
my guess, without seeing specific error is that their paths were entered incorrectly.
buran likes this post
Reply
#5
(Mar-27-2025, 02:19 PM)invisiblemind Wrote: When they run the script, they just get a temporary window popup.
Most likely they have .py files associated with python and run it with double click. Ask them to run it from command line/shell and get the output.
It's just fine to run it on one machine and process files on network drive/server.

Given it's a CLI I wouldn't use tkinter just for a pop-up message box.
You can use loguru (https://github.com/Delgan/loguru) to easily create log/output progress info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Detect if another copy of a script is running from within the script gw1500se 4 1,138 Jan-31-2025, 11:30 PM
Last Post: Skaperen
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,374 May-03-2024, 07:23 AM
Last Post: Pedroski55
  No Internet connection when running a Python script basil_555 8 3,328 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 6,761 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 1,066 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Help Running Python Script in Mac OS emojistickers 0 960 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  Trying to make a board with turtle, nothing happens when running script Quascia 3 1,776 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Python script running under windows over nssm.exe JaroslavZ 0 1,775 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Running script with subprocess in another directory paul18fr 1 12,369 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 2,024 Aug-03-2022, 06:03 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020