Code: Alles auswählen
MEDIA_PFAD = Path("/media/earl/")
def main():
umount_result = subprocess.run(["umount", MEDIA_PFAD / "SEAGULL"], check=True, stdout=subprocess.PIPE)
print(umount_result.stdout)
if __name__ == "__main__":
main()
Code: Alles auswählen
MEDIA_PFAD = Path("/media/earl/")
def main():
umount_result = subprocess.run(["umount", MEDIA_PFAD / "SEAGULL"], check=True, stdout=subprocess.PIPE)
print(umount_result.stdout)
if __name__ == "__main__":
main()
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pathlib import Path
import pyudev
import time
#import pathlib
from datetime import datetime as DateTime
import subprocess
PFAD = Path.home() / ".DruckData"
###----wird später gebraucht------
##ABFRAGE_FILENAME = PFAD / "TB_Ausgabe_Abfrage8StueckII.txt"
##ZAEHLER_FILENAME = PFAD / "numbers.csv"
###------------------------------
MEDIA_PFAD = Path('/media/earl/')
WECHSEL_DATEI_NAMEN = ["numbers.csv", "TB_Ausgabe_8iii.txt"]
print(MEDIA_PFAD)
context = pyudev.Context()
def usb_ansteckerkenner():
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block')
for device in iter(monitor.poll, None):
if 'ID_FS_TYPE' in device: ###
print(device.action)
if device.action == 'add':
name_of_stick = Path(device.get('ID_FS_LABEL'))
print(name_of_stick)
time.sleep(2)
return name_of_stick
def copy(name_of_stick, source_path, destination_path ):
try:
text = source_path.read_text(encoding="utf-8")
destination_path.write_text(text, encoding="utf-8")
except FileNotFoundError as error:
rauswerfer(name_of_stick)
print(error)
def datei_auf_stick(name_of_stick, dateipfad, timestamp):
copy(name_of_stick,
PFAD / dateipfad,
MEDIA_PFAD
/ name_of_stick
/ dateipfad.with_name(
f"{dateipfad.stem}_{timestamp:%Y-%m-%d_%H_%M}.csv"
),
)
def datei_auf_arbeitsverzeichnis(name_of_stick, dateipfad):
copy(name_of_stick, MEDIA_PFAD / name_of_stick / dateipfad, PFAD / dateipfad)
def rauswerfer(name_of_stick):
print(name_of_stick)
umount_result = subprocess.run(["umount", MEDIA_PFAD / name_of_stick], check=True, stdout=subprocess.PIPE)
print(umount_result.stdout)
def main():
timestamp = DateTime.now()
name_of_stick = usb_ansteckerkenner()
for dateiname in map(Path, WECHSEL_DATEI_NAMEN):
datei_auf_stick(name_of_stick, dateiname, timestamp)
datei_auf_arbeitsverzeichnis(name_of_stick, dateiname)
if __name__ == "__main__":
main()
Code: Alles auswählen
def rauswerfer(name_of_stick):
print(name_of_stick)
umount_result = subprocess.run(["umount", MEDIA_PFAD / name_of_stick], check=True, stdout=subprocess.PIPE)
print(umount_result.stdout)
main()
Code: Alles auswählen
from pathlib import Path
import pyudev
import time
#import pathlib
from datetime import datetime as DateTime
import subprocess
PFAD = Path.home() / ".DruckData"
###----wird später gebraucht------
##ABFRAGE_FILENAME = PFAD / "TB_Ausgabe_Abfrage8StueckII.txt"
##ZAEHLER_FILENAME = PFAD / "numbers.csv"
###------------------------------
MEDIA_PFAD = Path('/media/earl/')
WECHSEL_DATEI_NAMEN = ["numbers.csv", "TB_Ausgabe_8iii.txt"]
print(MEDIA_PFAD)
def usb_ansteckerkenner(context):
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block')
for device in iter(monitor.poll, None):
if 'ID_FS_TYPE' in device: ###
print(device.action)
if device.action == 'add':
name_of_stick = Path(device.get('ID_FS_LABEL'))
print(name_of_stick)
time.sleep(2)
return name_of_stick
def copy(name_of_stick, source_path, destination_path ):
try:
text = source_path.read_text(encoding="utf-8")
destination_path.write_text(text, encoding="utf-8")
except FileNotFoundError as error:
rauswerfer(name_of_stick)
print(error)
def datei_auf_stick(name_of_stick, dateipfad, timestamp):
copy(name_of_stick,
PFAD / dateipfad,
MEDIA_PFAD
/ name_of_stick
/ dateipfad.with_name(
f"{dateipfad.stem}_{timestamp:%Y-%m-%d_%H_%M}.csv"
),
)
def datei_auf_arbeitsverzeichnis(name_of_stick, dateipfad):
copy(name_of_stick, MEDIA_PFAD / name_of_stick / dateipfad, PFAD / dateipfad)
def rauswerfer(name_of_stick):
print(name_of_stick)
umount_result = subprocess.run(["umount", MEDIA_PFAD / name_of_stick], check=True, stdout=subprocess.PIPE)
print(umount_result.stdout)
def main():
context = pyudev.Context()
while True:
timestamp = DateTime.now()
name_of_stick = usb_ansteckerkenner(context)
for dateiname in map(Path, WECHSEL_DATEI_NAMEN):
datei_auf_stick(name_of_stick, dateiname, timestamp)
datei_auf_arbeitsverzeichnis(name_of_stick, dateiname)
if __name__ == "__main__":
main()
@__deets__ Leider geht das so nicht.
Code: Alles auswählen
================= RESTART: /home/earl/projekt/USB/usb.iix.py =================
/media/earl
add
SEAGULL
SEAGULL
b''
[Errno 2] No such file or directory: '/media/earl/SEAGULL/TB_Ausgabe_8iii.txt'
remove
Code: Alles auswählen
================= RESTART: /home/earl/projekt/USB/usb.iix.py =================
/media/earl
add
SEAGULL
SEAGULL
b''
[Errno 2] No such file or directory: '/media/earl/SEAGULL/numbers.csv'
SEAGULL
Traceback (most recent call last):
File "/home/earl/projekt/USB/usb.iix.py", line 38, in copy
destination_path.write_text(text, encoding="utf-8")
File "/usr/lib/python3.6/pathlib.py", line 1215, in write_text
with self.open(mode='w', encoding=encoding, errors=errors) as f:
File "/usr/lib/python3.6/pathlib.py", line 1183, in open
opener=self._opener)
File "/usr/lib/python3.6/pathlib.py", line 1037, in _opener
return self._accessor.open(self, flags, mode)
File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: '/media/earl/SEAGULL/TB_Ausgabe_8iii_2020-03-29_17_08.csv'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/earl/projekt/USB/usb.iix.py", line 79, in <module>
main()
File "/home/earl/projekt/USB/usb.iix.py", line 74, in main
datei_auf_stick(name_of_stick, dateiname, timestamp)
File "/home/earl/projekt/USB/usb.iix.py", line 50, in datei_auf_stick
f"{dateipfad.stem}_{timestamp:%Y-%m-%d_%H_%M}.csv"
File "/home/earl/projekt/USB/usb.iix.py", line 40, in copy
rauswerfer(name_of_stick)
File "/home/earl/projekt/USB/usb.iix.py", line 62, in rauswerfer
umount_result = subprocess.run(["umount", MEDIA_PFAD / name_of_stick], check=True, stdout=subprocess.PIPE)
File "/usr/lib/python3.6/subprocess.py", line 438, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['umount', PosixPath('/media/earl/SEAGULL')]' returned non-zero exit status 1.
>>>
Das ist jetzt schön einfach dahingesagt. Was hast du denn alternativ für eine Idee?Es zeigt sich hier, dass keine gute Idee ist, wenn eine Datei nicht gefunden wurde, zu vermuten, dass es dann sinnvoll sein kann, umount aufzurufen.
Das stimmt so auch nicht ganz.Naja, hast Du mal gelesen, was Du da zusammenprogrammiert hast?
1. Du wartest bis ein USB-Stick eingesteckt wird. Soweit ok.
2. Du kopierst eine Datei vom Home-Verzeichnis auf den Stick. Wenn diese Datei nicht gefunden wird, wirst Du den Stick aus? Warum?
3. Du kopierst eine Datei vom Stick ins Home-Verzeichnis. Auch hier wieder, wenn die Datei nicht gefunden wird, wirst Du den Stick aus. Bei Fehler in 2 ist der Stick aber schon ausgeworfen, also kommt da der nächste Fehler.
Weil drittens nach einem Fehler in zweitens keinen Sinn mehr macht.