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!!

New contributor
Saqib Shakeel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Why don't you use ftp like the rest of us? – Milliways Feb 11 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 :( – Saqib Shakeel Feb 12 at 8:58
  • Then you should state this in your question. As it stands I don't know what you are asking. – Milliways Feb 12 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') – Saqib Shakeel Feb 12 at 21:28
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).

  • 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') – Saqib Shakeel Feb 12 at 21:30
  • @SaqibShakeel I forgot NFS, which I assume one of these directories is mounted with? – v25 Feb 12 at 21:34
  • yeah @v25 you are right :) – Saqib Shakeel Feb 12 at 22:12

Your Answer

Saqib Shakeel is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.