2020-09-26, 19:53
Hier ein kurzes Beispiel, wie man mit Python-Modulen Dateien kopieren, zippen und löschen kann.
import zipfile
from shutil import copyfile
from os import unlink
# copy file
copyfile('dtk-authoryear.bbx' , './dtk-bibliography/dtk-authoryear.bbx')
copyfile('dtk-authoryear.dbx' , './dtk-bibliography/dtk-authoryear.dbx')
copyfile('dtk-bibliography.pdf', './dtk-bibliography/dtk-bibliography.pdf')
copyfile('dtk-bibliography.tex', './dtk-bibliography/dtk-bibliography.tex')
# create the zip file
with zipfile.ZipFile('dtk-bibliography.zip', 'w', zipfile.ZIP_DEFLATED) as z:
z.write('./dtk-bibliography/README.md')
z.write('./dtk-bibliography/dtk-authoryear.bbx')
z.write('./dtk-bibliography/dtk-authoryear.dbx')
z.write('./dtk-bibliography/dtk-bibliography.pdf')
z.write('./dtk-bibliography/dtk-bibliography.tex')
# delete copied files
unlink('./dtk-bibliography/dtk-authoryear.bbx')
unlink('./dtk-bibliography/dtk-authoryear.dbx')
unlink('./dtk-bibliography/dtk-bibliography.pdf')
unlink('./dtk-bibliography/dtk-bibliography.tex') |
import zipfile
from shutil import copyfile
from os import unlink
# copy file
copyfile('dtk-authoryear.bbx' , './dtk-bibliography/dtk-authoryear.bbx')
copyfile('dtk-authoryear.dbx' , './dtk-bibliography/dtk-authoryear.dbx')
copyfile('dtk-bibliography.pdf', './dtk-bibliography/dtk-bibliography.pdf')
copyfile('dtk-bibliography.tex', './dtk-bibliography/dtk-bibliography.tex')
# create the zip file
with zipfile.ZipFile('dtk-bibliography.zip', 'w', zipfile.ZIP_DEFLATED) as z:
z.write('./dtk-bibliography/README.md')
z.write('./dtk-bibliography/dtk-authoryear.bbx')
z.write('./dtk-bibliography/dtk-authoryear.dbx')
z.write('./dtk-bibliography/dtk-bibliography.pdf')
z.write('./dtk-bibliography/dtk-bibliography.tex')
# delete copied files
unlink('./dtk-bibliography/dtk-authoryear.bbx')
unlink('./dtk-bibliography/dtk-authoryear.dbx')
unlink('./dtk-bibliography/dtk-bibliography.pdf')
unlink('./dtk-bibliography/dtk-bibliography.tex')
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2014-02-19, 21:26
Here’s a short code sample how to list a remote directory via FTP.
from ftplib import FTP
ftp = FTP('server')
ftp.connect()
ftp.login('user','password')
ftp.cwd('folder')
ftp.dir()
ftp.quit() |
from ftplib import FTP
ftp = FTP('server')
ftp.connect()
ftp.login('user','password')
ftp.cwd('folder')
ftp.dir()
ftp.quit()
Hier noch der passende Code für den Upload von Dateien:
import ftplib
import os
def upload(ftp, file):
ext = os.path.splitext(file)[1]
print("Putting ", file)
if ext in (".txt", ".htm", ".html" , ".tex"):
ftp.storlines('STOR ' + file, open(file,'rb'))
else:
ftp.storbinary('STOR ' + file, open(file,'rb'))
ftp = ftplib.FTP("remoteserver")
ftp.login("user", "password")
upload(ftp, "index.html") |
import ftplib
import os
def upload(ftp, file):
ext = os.path.splitext(file)[1]
print("Putting ", file)
if ext in (".txt", ".htm", ".html" , ".tex"):
ftp.storlines('STOR ' + file, open(file,'rb'))
else:
ftp.storbinary('STOR ' + file, open(file,'rb'))
ftp = ftplib.FTP("remoteserver")
ftp.login("user", "password")
upload(ftp, "index.html")
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website