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

  • mipd1980

    mipd1980

    25 EKİM 2006
  • Nickcidious

    Nickcidious

    6 HAZİRAN 2011
  • Shylo Sharity

    Shylo Sharit

    27 EKİM 2011