You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派通过Python传输CSV文件至Windows的命令行方案咨询

Python Command-Line Methods to Transfer CSV from Raspberry Pi to Windows

Absolutely, there are several reliable Python-based command-line approaches to move your sensor CSV files from Raspberry Pi to a Windows machine. Below are the most practical options, assuming both devices are on the same local network:

1. SFTP Transfer (Using paramiko)

Since modern Windows 10/11 comes with OpenSSH Server pre-installed (you just need to enable it), SFTP is a secure, straightforward choice.

Step 1: Prep

  • On Windows: Go to Settings > Apps > Optional features > Add a feature, search for "OpenSSH Server", install it, then start the service via PowerShell:
    Start-Service sshd
    Set-Service -Name sshd -StartupType 'Automatic'
    
  • On Raspberry Pi: Install the paramiko library:
    pip3 install paramiko
    

Step 2: Command-Line Python Script

Create a script (e.g., sftp_transfer.py) with the following code, which uses argparse to accept command-line arguments:

import argparse
import paramiko

def transfer_csv(local_path, win_ip, win_user, win_pass, remote_path):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(win_ip, username=win_user, password=win_pass)
        sftp = ssh.open_sftp()
        sftp.put(local_path, remote_path)
        print(f"Successfully transferred {local_path} to {win_ip}:{remote_path}")
    finally:
        sftp.close()
        ssh.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Transfer CSV from Raspberry Pi to Windows via SFTP')
    parser.add_argument('local_csv', help='Path to local CSV file on Pi (e.g., /home/pi/sensor_data.csv)')
    parser.add_argument('win_ip', help='Windows machine IP address (e.g., 192.168.1.100)')
    parser.add_argument('win_user', help='Windows username (e.g., JohnDoe)')
    parser.add_argument('win_pass', help='Windows password')
    parser.add_argument('remote_path', help='Target path on Windows (e.g., C:/Users/JohnDoe/Documents/sensor_data.csv)')
    args = parser.parse_args()
    
    # Convert Windows path to SFTP-compatible format (replace backslashes with forward slashes)
    remote_path = args.remote_path.replace("\\", "/")
    transfer_csv(args.local_csv, args.win_ip, args.win_user, args.win_pass, remote_path)

Run It

Execute the script from your Pi's terminal:

python3 sftp_transfer.py /home/pi/sensor_data.csv 192.168.1.100 JohnDoe MyWinPass123 C:/Users/JohnDoe/Documents/sensor_data.csv

2. Temporary HTTP Server (No Extra Libraries Needed)

If you want a zero-install option on the Pi (uses Python's built-in http.server), you can host the CSV file briefly and download it from Windows.

Step 1: Host the File on Pi

Navigate to the folder containing your CSV and run:

python3 -m http.server 8000

This starts a simple web server on port 8000.

Step 2: Download from Windows

Open PowerShell on Windows and run:

Invoke-WebRequest -Uri http://<PI_IP_ADDRESS>:8000/sensor_data.csv -OutFile "C:\Users\YourUser\Documents\sensor_data.csv"

Or use curl if you prefer:

curl http://<PI_IP_ADDRESS>:8000/sensor_data.csv -o "C:\Users\YourUser\Documents\sensor_data.csv"

Once the download finishes, stop the Pi's server with Ctrl+C.

3. SMB File Share Transfer (Using pysmb)

If you have a shared folder set up on Windows, you can use the SMB protocol to push the file directly to it.

Step 1: Prep

  • On Windows: Create a shared folder, grant write permissions to your Windows user (or "Everyone" for testing).
  • On Raspberry Pi: Install pysmb:
    pip3 install pysmb
    

Step 2: Command-Line Script

Create smb_transfer.py:

import argparse
from smb.SMBConnection import SMBConnection

def transfer_csv(local_path, win_ip, win_user, win_pass, share_name, remote_filename):
    conn = SMBConnection(win_user, win_pass, "raspberrypi", "WINDOWSPC", use_ntlm_v2=True)
    if conn.connect(win_ip, 139):
        with open(local_path, 'rb') as f:
            conn.storeFile(share_name, remote_filename, f)
        print(f"Successfully transferred {local_path} to \\\\{win_ip}\\{share_name}\\{remote_filename}")
        conn.close()
    else:
        print("Failed to connect to Windows SMB share")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Transfer CSV from Raspberry Pi to Windows SMB share')
    parser.add_argument('local_csv', help='Path to local CSV file on Pi')
    parser.add_argument('win_ip', help='Windows machine IP')
    parser.add_argument('win_user', help='Windows username')
    parser.add_argument('win_pass', help='Windows password')
    parser.add_argument('share_name', help='Name of the Windows shared folder')
    parser.add_argument('remote_filename', help='Name to save the file as on the share')
    args = parser.parse_args()
    
    transfer_csv(args.local_csv, args.win_ip, args.win_user, args.win_pass, args.share_name, args.remote_filename)

Run It

python3 smb_transfer.py /home/pi/sensor_data.csv 192.168.1.100 JohnDoe MyWinPass123 SensorDataShare sensor_data.csv

All these methods work from the command line, so you can even automate them (e.g., add the transfer script to your sensor data collection cron job on the Pi).

内容的提问来源于stack exchange,提问作者user7332748

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:28:51