0

I'm playing around with python3+ but yet can't get it to read a file thats local to the git repo

output

pyyaml version = 5.3.1
Whoops! Can't find config.yml file --- exiting

Process finished with exit code 1

file permissions

-rw-r--r-- 1 pi pi  270 Nov 29 18:55 config.yml
drwxr-xr-x 8 pi pi 4096 Nov 30 12:54 .git
drwxr-xr-x 3 pi pi 4096 Nov 30 13:00 .idea
-rw-r--r-- 1 pi pi  447 Nov 30 13:19 main.py

app code

import requests
import yaml
import sys
from bs4 import BeautifulSoup


ymlversion = yaml.__version__
print("pyyaml version = " + ymlversion)


try:
    open("config.yml")
except FileNotFoundError:
    sys.exit('Whoops! Can\'t find config.yml file --- exiting')

# open the yaml file and load it into data
with open("config.yml", "r") as yamlfile:
    data = yaml.load(yamlfile, Loader=yaml.FullLoader)
    print("Read successful")
print(data)
khelwood
  • 55,782
  • 14
  • 81
  • 108
Tony
  • 8,681
  • 7
  • 36
  • 55
  • 3
    Do you run your script from the Git directory ? – Frodon Nov 30 '20 at 18:32
  • What happens when you comment out the `try` block and run the `with` block? – sphennings Nov 30 '20 at 18:33
  • Remember that `config.yal` is relevant to your current _working_ directory, not necessarily the directory within which `main.py` is located. – Harrison Totty Nov 30 '20 at 18:33
  • If your first `open` succeeds, you won't close it, so you'll have the file open twice once your code can find the file. I'd suggest that you use `os.path.isfile()` to check for the existence of the file. You might want to do `print(os.getcwd())` to see what your current directory is, so you know what directory Python is looking for your input file in. – CryptoFool Nov 30 '20 at 18:37

2 Answers2

0

If the yaml file is always next to the python file, you should code it:

import os
import yaml
import sys
from bs4 import BeautifulSoup

SCRIPTDIR = os.path.dirname(__file__)
YAMLFILE = os.path.join(SCRIPTDIR, 'config.yaml')

ymlversion = yaml.__version__
print("pyyaml version = " + ymlversion)

# Old way to check if file exists:
'''
try:
    open(YAMLFILE)
except FileNotFoundError:
    sys.exit('Whoops! Can\'t find config.yml file --- exiting')
'''

# Better way to check if file exists:
if not os.path.exists(YAMLFILE):
    sys.exit('Whoops! Can\'t find config.yml file --- exiting')

# open the yaml file and load it into data
with open(YAMLFILE, "r") as yamlfile:
    data = yaml.load(yamlfile, Loader=yaml.FullLoader)
    print("Read successful")
print(data)
Frodon
  • 3,684
  • 1
  • 16
  • 33
  • 1
    I'd suggest that you change the first `open` to `os.path.exists()`, since it's otherwise a filehandle leak. You could show the OP how to avoid that and the try/catch. – CryptoFool Nov 30 '20 at 18:42
0

config.yml is read from the current working directory, which is the directory you run it from. So, either run the script from the directory, or use __file__ (the path of the script file) and os.chdir to change your cwd:

import os

os.chdir(os.path.dirname(__file__))

You could also read the file from the path os.path.join(os.path.dirname(__file__), "config.yml").

os.path.join and os.path.dirname are path manipulation functions that just join paths together and gets their directory name, respectively.

Aplet123
  • 33,825
  • 1
  • 29
  • 55