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!!
1 Answer 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– Saqib Shakeel2019年02月12日 21:30:26 +00:00Commented Feb 12, 2019 at 21:30
-
@SaqibShakeel I forgot NFS, which I assume one of these directories is mounted with?v25– v252019年02月12日 21:34:23 +00:00Commented Feb 12, 2019 at 21:34
-
yeah @v25 you are right :)Saqib Shakeel– Saqib Shakeel2019年02月12日 22:12:04 +00:00Commented Feb 12, 2019 at 22:12
ftp
like the rest of us?