SORU
24 Mayıs 2011, Salı


Python olumsuzluk

Eğer bir yolu yok ise, ancak bir dizin oluşturmak için çalışıyorum. (değil) operatörü işe yaramıyor. Python inkar etmek nasıl emin değilim... Ne bu yapmak için doğru bir yolu var mı?

if (!os.path.exists("/usr/share/sounds/blues")):
        proc = subprocess.Popen(["mkdir", "/usr/share/sounds/blues"])
        proc.wait()

CEVAP
24 Mayıs 2011, Salı


Python olumsuzluk operatörü not. Bu nedenle sadece ** 5 not ile değiştirin.

Eğer, örneğin, bunu yapmak için:

if not os.path.exists("/usr/share/sounds/blues") :
    proc = subprocess.Popen(["mkdir", "/usr/share/sounds/blues"])
    proc.wait()

İçin spesifik bir örnek olarak Neil dedi yorum) gerek yok kullanmak için subprocess modülü, kolay kullanım os.mkdir() almak için neden ihtiyacın var, ilave özel durum işleme iyilik.

Örnek:

blues_sounds_path = "/usr/share/sounds/blues"
if not os.path.exists(blues_sounds_path):
    try:
        os.mkdir(blues_sounds_path)
    except OSError:
        # Handle the case where the directory could not be created.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • H3Ctic (old channel)

    H3Ctic (old

    23 Mart 2011
  • Mismag822 - The Card Trick Teacher

    Mismag822 -

    18 EKİM 2008
  • TecnoTutosPC

    TecnoTutosPC

    19 Kasım 2012