0

I want to transfer text files or a complete folder containing text files from my computer to raspberry pi using a python script.

I am accessing my raspberry from the same computer (Using Putty) from where I have to copy files.

Now I am not understanding how to link the destination and source using python to make the transfer happen. I am beginner to linux and python. Anyone who can help me out? would be thankful!!

asked Feb 11, 2019 at 20:34
4
  • Why don't you use ftp like the rest of us? Commented Feb 11, 2019 at 21:52
  • I want to copy files from 4 different computers to a hard disk mounted with raspberry pi. And it should be automatic e.g once a day at some specific time, my files starts copying from PC to the Hard disk. Makes sence? I am not sure if I am doing it right or not as I am new to this :( Commented Feb 12, 2019 at 8:58
  • Then you should state this in your question. As it stands I don't know what you are asking. Commented Feb 12, 2019 at 11:33
  • Thanks for your answers, This program runs for me :) import os, shutil path = '/mnt/pc49/' copyto= '/share/Festplatte/Saqib/' files = os.listdir(path) files.sort() for f in files: src = path+f dst = copyto+f shutil.copy(src,dst) print('Files Copied') Commented Feb 12, 2019 at 21:28

1 Answer 1

1

Non Python

The first way you could copy the file is over ssh. If you've already got access to the Pi via Putty then the server-side is already working. Just grab the pscp.exe binary from the official site. Watch out there's a similar one called pSftp which is probably not what you want.

Then do the copy like this:

Windows> pscp c:\documents\info.txt pi@raspberry:/tmp/info.txt

There's a step-by-step guide here.

For recursive copies of a directory, you sould use the -r flag:

Windows> pscp -r c:\documents\ pi@raspberry:/home/pi

This one copies the whole c:\documents folder to your home directory on the Pi.

The command uses encryption by default.

Python

There's probably a number of ways to implement this, which could have pros or cons depending on your requirements.

Natively

You could use Python's native low level networking interface, socket for device to device communication. One tutorial demonstrates a configuration to download files from EC2. This would probably be benificial to learning Python.

Won't be encrypted by default.

With a web framework.

If you want something that be accessed over HTTP, then you may look at a framework like Flask to implement a file upload facility. A very basic script I made can accept a single file-upload with the following curl command:

curl -i -X POST -F 'file=@upload_me.txt' "http://localhost:5000/upload" -H 'ContentType: multipart/form-data'

Or you could go further with it and add HTML templates, so it can be accessed through a web-browser, providing a file upload form.

This configuration is not encrypted (but can be with SSL certs from somewhere like Let's Encrypt, you'd also eventually run the application with gunicorn instead of Python, and preferably behind nginx).

answered Feb 11, 2019 at 22:15
3
  • Thanks, THe following code is working for me :) import os, shutil path = '/mnt/pc49/' copyto= '/share/Festplatte/Saqib/' files = os.listdir(path) files.sort() for f in files: src = path+f dst = copyto+f shutil.copy(src,dst) print('Files Copied') Commented Feb 12, 2019 at 21:30
  • @SaqibShakeel I forgot NFS, which I assume one of these directories is mounted with? Commented Feb 12, 2019 at 21:34
  • yeah @v25 you are right :) Commented Feb 12, 2019 at 22:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.