使用 os.makedirs 在 Python 脚本中无法正确创建目录

Directory is Not Getting Made Correctly in Python Script with os.makedirs

我有一个 python 脚本,可以从 AWS SSO 应用程序中抓取信誉,它使用 Selenium 和 Chromedriver 工作。整个脚本可以工作,甚至可以下载 Webdriver,只是没有在正确的位置执行,因为它被调用的地方。相反,它会下载脚本在文件系统中存在或驻留的任何位置。

确保目录的代码块:

def ensure_dir():
    if platform.system() == ('Windows'):
        if not os.path.exists("~\Program Files (x86)\Google\Chrome"):
            os.makedirs("~\Program Files (x86)\Google\Chrome")
            print("Made Directory for Chrome Driver")
            if chrome_version() == ('84.0.4147.30'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=84.0.4147.30/"
                wget.download(url, '~\Program Files (x86)\Google\Chrome')
            elif chrome_version() == ('85.0.4183.87'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=85.0.4183.87/"
                wget.download(url, '~\Program Files (x86)\Google\Chrome')
            elif chrome_version() == ('86.0.4240.22'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/"
                wget.download(url, '~\Program Files (x86)\Google\Chrome')
            else:
                print("The Driver is Already Downloaded...")
                print("Driver Installation Checked")
    elif platform.system() == ('Linux'):
        if not os.path.exists("~/Drivers/Google/Chrome/chromedriver"):
            os.makedirs("~/Drivers/Google/Chrome/chromedriver")
            print("Made Directory for Chrome Driver")
            if chrome_version() == ('84.0.4147.30'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=84.0.4147.30/"
                wget.download(url, '~/Drivers/Google/Chrome/chromedriver')
            elif chrome_version() == ('85.0.4183.87'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=85.0.4183.87/"
                wget.download(url, '~/Drivers/Google/Chrome/chromedriver')
            elif chrome_version() == ('86.0.4240.22'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/"
                wget.download(url, '~/Drivers/Google/Chrome/chromedriver')
            else:
                print("The Driver is Already Downloaded...")
                print("Driver Installation Checked")
    elif platform.system() == ('Darwin'):
        if not os.path.exists("~/Drivers/Google/Chrome/chromedriver"):
            os.makedirs("~/Drivers/Google/Chrome/chromedriver")
            print("Made Directory for Chrome Driver")
            if chrome_version() == ('84.0.4147.30'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=84.0.4147.30/"
                wget.download(url, '~/Drivers/Google/Chrome/chromedriver')
            elif chrome_version() == ('85.0.4183.87'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=85.0.4183.87/"
                wget.download(url, '~/Drivers/Google/Chrome/chromedriver')
            elif chrome_version() == ('86.0.4240.22'):
                url = "https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/"
                wget.download(url, '~/Drivers/Google/Chrome/chromedriver')
            else:
                print("The Driver is Already Downloaded...")
                print("Driver Installation Checked")
    else:
        print('System && Chrome Driver Checks Instantiated')
        print('Checks Not Passed Exiting...')
        raise SystemExit

ensure_dir()

但是,Windows、Linux 和 macOS 下不会下载:

windows = ~\Program Files (x86)\Google\Chrome
macOS = ~/Drivers/Google/Chrome/chromedriver
Linux = ~/Drivers/Google/Chrome/chromedriver

相反,它会在脚本所在的同一目录中下载:

 rbarrett@MacBook-Pro  ~/Projects/Mirantis/Train  tree                                         ✔  3993  09:39:37
.
├── AWS_Scraping.side
├── cookiejar
├── rbarrett-test
│   ├── key-pairs.txt
│   ├── users
│   │   └── rbarrett
│   │       ├── dtr-1.txt
│   │       ├── dtr-2.txt
│   │       ├── rbarrett-rbarrett-test.pem
│   │       ├── rbarrett-rbarrett-test.ppk
│   │       ├── ucp-1.txt
│   │       └── ucp-2.txt
│   └── users.cfg
├── requirements.txt
├── scrape_creds.py
├── secrets.bac
├── secrets.json
├── train.bac
├── train.env
└── ~
    └── Drivers
        └── Google
            └── Chrome
                └── chromedriver

看起来它是在工作目录而不是主目录中创建的。 有什么办法可以解决这个问题吗?

~ 是一个字符串,因此不会按预期进行计算。
您可以使用 expanduser 获取用户的主目录:

import os

def ensure_dir():
    home = os.path.expanduser("~")
    if platform.system() == ('Windows'):
        chromeDir = os.path.join(home, '/Drivers/Google/Chrome/chromedriver')
        if not os.path.exists(chromeDir):
            ...