Skip to main content

An easy way to manage file creation.

Project description

Ensure File

An easy way to manage file creation

Example usage:

from ensure_file import ensure_file as ef

# This will create a file in the user directory under the `.example` folder
# if the file doesn't already exist. If the file does exist, nothing happens.
# Along with that, it will return true if a file is created, and false if
# nothing happens. This applies to everything else going forward.
ef("~/.example/foo.txt")

# Using the `default_value` parameter, you can set the default value to be
# written to the file. If the file already exists, nothing happens.
import json
ef("./bar.json", default_value=json.dumps({
  "foo": "bar"
}))

# If you want to create a folder, you can set the `folder` parameter to true.
ef("/tmp/foo", folder=True)

# If there is already a folder with the same name as a file you want to create,
# the folder will be deleted and overwritten. This can be disabled by setting
# the `auto_correct_type` to false. In this function call, nothing would happen
# because of the folder created on the previous line.
ef("/tmp/foo", folder=False, auto_correct_type=False)

# If you want to not actually create any files or folders and instead throw an
# error if a file or folder doesn't exist, you can easily do so by setting the
# `throw_error` parameter to true. By default, this will also throw an error
# if the file or folder is the wrong type, such as checking if the file you're
# checking has the same name as a folder in the same directory. The type
# checking can be disabled by setting the `throw_check_type` parameter to
# false.
try:
    ef("./some/file.css", folder=False, throw_error=True)
    print("The file exists!")

except FileNotFoundError:
    print("The file doesn't exist!")
except FileExistsError:
    print("A folder exists where there should be a file!")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ensure_file-1.1.1.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ensure_file-1.1.1-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file ensure_file-1.1.1.tar.gz.

File metadata

  • Download URL: ensure_file-1.1.1.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for ensure_file-1.1.1.tar.gz
Algorithm Hash digest
SHA256 f921d6681d00b6bc4ecefd2eec1612a5f72c1e4b74f4726d367be622940fcd4f
MD5 2181c0b3fcba58876308dc2502849380
BLAKE2b-256 2b6039ce660f27cadda7b79230c3b498b0d653ceb2c21bcc851ce73b1d4e0d78

See more details on using hashes here.

File details

Details for the file ensure_file-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: ensure_file-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for ensure_file-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc6c68c06ecbc502604c20372bd4911fffff9e1d4cbaae6134827cda8f3085dc
MD5 ab6581d78ebb845724d981b1d8157f88
BLAKE2b-256 7d8b5f796b77c9006390083c7cc65bd3f606babbfa0695d6dff5c7a516baea8d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page