Skip to content

Runtime filepath defining instead of fixed filepath for text files. #1

Closed
@rowds

Description

@rowds

Importing this as is from a python project's modules' directory that is 'project_dir/sample/' will raise a filepath error. It is very simple to fix this issue but will greatly increase the usabilty of the code. Instead of defining fixed file paths for text files like this:

FirstNames    = './Names/FirstNames.txt'
MiddleNames   = './Names/MiddleNames.txt'
LastNames     = './Names/LastNames.txt'
CountyNames   = './Names/CountyNames.txt'
PlaceNames    = './Names/PlaceNames.txt'
StateNames    = './Names/StateNames.txt'
CountryNames  = './Names/CountryNames.txt'
CompanyNames  = './Names/CompanyNames.txt'

Define the filepaths in runtime instead so that it can run as a importable module or a standalone script or whatever, like this:

#directory from where the module is running currently.
script_dir    = os.path.dirname(__file__)
names_dir     = os.path.join(module_dir, 'Names')

FirstNames    = os.path.join(names_dir, 'FirstNames.txt')
MiddleNames   = os.path.join(names_dir, 'MiddleNames.txt')
LastNames     = os.path.join(names_dir, 'LastNames.txt')
CountyNames   = os.path.join(names_dir, 'CountyNames.txt')
PlaceNames    = os.path.join(names_dir, 'PlaceNames.txt')
StateNames    = os.path.join(names_dir, 'StateNames.txt')
CountryNames  = os.path.join(names_dir, 'CountryNames.txt')
CompanyNames  = os.path.join(names_dir, 'CompanyNames.txt')

P.S. the Names folder still needs to be in the same directory as the script.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions