简体   繁体   中英

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

I have a python Script that scrapes the creds from an AWS SSO Application, it works using Selenium and Chromedriver. The overall script works and even downloads the Webdriver, it just doesn't do it in the correct place, for where it is invoked. Instead, it downloads wherever the script is living or residing within the file system.

Code Block That Ensures Directory:

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()

However, it doesn't download in the following for Windows, Linux, and macOS:

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

Instead, it downloads in the same directory wherever the script resides:

 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

It looks like it creates it in the working directory instead of the home directory. Is there a way in which I can fix this?

~ is a string, so it is not going the evaluated as expected.
You can make use of expanduser to get the user's home directory:

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):
            ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM