aphonsoar / Receita_Federal_do_Brasil_-_Dados_Publicos_CNPJ

Dados Públicos de CNPJ disponibilizados pela Receita Federal do Brasil
MIT License
253 stars 108 forks source link

Improve way to deal with file paths on Windows, Mac and Linux. And use environment variables. #5

Closed jeff-pal closed 3 years ago

jeff-pal commented 3 years ago

📋 Tasks Summary

  1. Declare imports in alphabetical order;
  2. Improve way to deal with file paths on Windows, Mac and Linux;
  3. Use environment variables.

👨‍💻 Solutions

Dealing with file paths on Windows, Mac and Linux

A easy way to deal with file paths is to use the python (3.4+) library pathlib. With pathlib we've just to use forward slashes (/). For example, the function Path() will convert forward slashes into the correct kind of slash for the current operating system:

from pathlib import Path
folder_path = Path("user/data")
print(folder_path)

Output:

#on Linux:
user/data

#on Windows:
user\data

If we need to add a sub path on to the path, we can use the / operator directly in the code:

from pathlib import Path
folder_path = Path("user/data")
print(folder_path / 'sub')

Output:

#on Linux:
user/data/sub

#on Windows:
user\data\sub
aphonsoar commented 3 years ago

Excelente contribuição! Obrigado @jeff-pal !