Nieuws:

We zijn er weer.

Na lange tijd van afwezigheid zijn we er weer  :laugh:
We hebben alle wachtwoorden gereset, je oude wachtwoord werkt niet meer.Je moet via het "wachtwoord vergeten"-linkje je wachtwoord resetten. Je krijgt hiervoor een mailtje op het adres dat je bij ons geregistreerd hebt.

De komende tijd zijn we nog druk bezig om de rest van de site op te bouwen, dus het kan zijn dat sommige onderdelen (tijdelijk) niet werken.

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: python3 os.path.exists  (gelezen 363 keer)

Offline daniel

  • Lid
  • Steunpunt: Ja
python3 os.path.exists
« Gepost op: 2015/03/09, 18:24:36 »
Een programma bedoeld om op meerdere distros te gebruiken.....

Ik wil graag zien of het programma bestaat voordat hij snellkoppelingen maakt maar naar de eerste if optie uitgevoerd te hebben slaat hij de rest van else over, hoe kan dit?

import os



if os.path.exists('/usr/bin/thunderbird'):
    print("0")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/thunderbird %U' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=thunderbird-Email' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=thunderbird' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/mail.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/mail.desktop")

elif os.path.exists('/usr/bin/lubuntu-software-center'):
    print("1")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/lubuntu-software-center %u' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Software-centrum' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=softwarecenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/software-centrum.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/software-centrum.desktop")

elif os.path.exists('/usr/bin/mintinstall'):
    print("2")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/mintinstall %u' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Software-centrum' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=softwarecenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/software-centrum.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/software-centrum.desktop")

elif os.path.exists('/usr/bin/software-center'):
    print("3")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/software-center %u' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Software-centrum' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=softwarecenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/software-centrum.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/software-centrum.desktop")

elif os.path.exists('/usr/bin/skype'):
    print("4")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=env PULSE_LATENCY_MSEC=60 skype %U' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=skype' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=skype' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/skype.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/skype.desktop ")


elif os.path.exists('/usr/bin/libreoffice'):
    print("5")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/libreoffice %U' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=libreoffice' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=libreoffice-startcenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/libreoffice.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/libreoffice.desktop")

elif os.path.exists('/usr/bin/bleachbit'):
    print("6")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")

    os.system("sudo sh -c 'echo Exec=su-to-root -X -c bleachbit' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Bleachbit' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=/usr/share/app-install/icons/bleachbit.png' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/bleachbit.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/bleachbit.desktop ")

elif os.path.exists('/usr/bin/gnome-commander'):
    print("7")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/gnome-commander' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=gnome-commander' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=gnome-commander' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/gnome-commander.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/gnome-commander.desktop")

elif os.path.exists('/usr/bin/update-manager'):
    print("8")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/update-manager' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=update-beheerder' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=update-manager' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/update-beheerder.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/update-beheerder.desktop ")

elif os.path.exists('/usr/bin/firefox'):
    print("9")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/firefox' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Firefox' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=firefox' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/firefox.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/firefox.desktop ")


     

Offline daniel

  • Lid
  • Steunpunt: Ja
Re: python3 os.path.exists
« Reactie #1 Gepost op: 2015/03/09, 19:17:19 »
als ik alles If doe dan werk het wel, waarom niet met Elif?

zo doet die het wel

import os



if os.path.exists('/usr/bin/thunderbird'):
    print("0")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/thunderbird %U' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=thunderbird-Email' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=thunderbird' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/mail.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/mail.desktop")

if os.path.exists('/usr/bin/lubuntu-software-center'):
    print("1")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/lubuntu-software-center %u' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Software-centrum' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=softwarecenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/software-centrum.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/software-centrum.desktop")

if os.path.exists('/usr/bin/mintinstall'):
    print("2")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/mintinstall %u' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Software-centrum' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=softwarecenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/software-centrum.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/software-centrum.desktop")

if os.path.exists('/usr/bin/software-center'):
    print("3")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/software-center %u' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Software-centrum' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=softwarecenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/software-centrum.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/software-centrum.desktop")

if os.path.exists('/usr/bin/skype'):
    print("4")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=env PULSE_LATENCY_MSEC=60 skype %U' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=skype' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=skype' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/skype.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/skype.desktop ")


if os.path.exists('/usr/bin/libreoffice'):
    print("5")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/libreoffice %U' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=libreoffice' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=libreoffice-startcenter' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/libreoffice.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/libreoffice.desktop")

if os.path.exists('/usr/bin/bleachbit'):
    print("6")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")

    os.system("sudo sh -c 'echo Exec=su-to-root -X -c bleachbit' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Bleachbit' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=/usr/share/app-install/icons/bleachbit.png' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/bleachbit.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/bleachbit.desktop ")

if os.path.exists('/usr/bin/gnome-commander'):
    print("7")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/gnome-commander' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=gnome-commander' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=gnome-commander' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/gnome-commander.desktop")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/gnome-commander.desktop")

if os.path.exists('/usr/bin/update-manager'):
    print("8")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/update-manager' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=update-beheerder' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=update-manager' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/update-beheerder.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/update-beheerder.desktop ")

if os.path.exists('/usr/bin/firefox'):
    print("9")
    os.system("sudo sh -c 'echo [Desktop Entry]' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Encoding=UTF-8' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Terminal=false' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Type=Application' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Exec=/usr/bin/firefox' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Name=Firefox' >> ~/snelkoppeling.desktop ")
    os.system("sudo sh -c 'echo Icon=firefox' >> ~/snelkoppeling.desktop ")
    os.system("mv $HOME/$user/snelkoppeling.desktop /$HOME/$user/Bureaublad/firefox.desktop ")
    os.system("sudo chmod 777 $HOME/$user/Bureaublad/firefox.desktop ")
else:

    print ("firefox is niet geinstalleerd")

     

Offline daniel

  • Lid
  • Steunpunt: Ja
Re: python3 os.path.exists
« Reactie #2 Gepost op: 2015/03/09, 19:35:00 »
ik weet dat je beter de write function kan gebruiken maar die moet ik nog schrijven, en dit was uit een bestaande script bash.
De vraag die hangen blijft is waarom de elif optie niet werkt want ook zou ik die nodig hebben met het zoeken van de juiste path.
Wie heeft er hier een antwoord op?

Offline erik1984

  • Lid
    • erik1984
  • Steunpunt: Nee
Re: python3 os.path.exists
« Reactie #3 Gepost op: 2015/03/09, 21:16:01 »
elif staat voor else if. Als aan de voorwaarde van de eerste if if is voldaan dus dan hoeft ie verder niet meer te kijken. Zelfde geldt als aan de tweede voorwaarde is voldaan etc.

Dit is eigenlijk jouw situatie:
if True:
    print("1")
elif True:
    print("2")
elif True:
    print("3")
Logisch dat daar 1 uitkomt en dat ie niet verder kijkt.

Offline daniel

  • Lid
  • Steunpunt: Ja
Re: python3 os.path.exists
« Reactie #4 Gepost op: 2015/03/09, 21:30:56 »
Stom van me, het is in eens duidelijk, ik heb een zware nacht gehad  =D

Dankjewel Erik....