Skip to content

Description:

  • Work with fake file data

Resources:

This notebook reads these environment variables. Set them before you start Jupyter:

  • SFTP_HOST: host name of an SFTP server you can write to.
  • SFTP_PORT: SFTP port (optional, defaults to 22).
  • SFTP_USERNAME: SFTP user name.
  • SFTP_PASSWORD: SFTP password.

Only Example 22 (SFTPStorage) needs these; skip that cell otherwise.

Create a text file with fake data

from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
faker = Faker()
faker.add_provider(TxtFileProvider)
# Usage example
txt_file = faker.txt_file(content="Lorem ipsum")
print(txt_file.data["filename"])
# The following line would produce a text generated by Faker, used as
# the content of the generated txt_file.
print(txt_file.data["content"])

Create a docx file with fake data

from faker import Faker
from faker_file import providers
from faker_file.providers.docx_file import DocxFileProvider
faker = Faker()
faker.add_provider(DocxFileProvider)
docx_file = faker.docx_file(
prefix="zzz",
max_nb_chars=1_024,
wrap_chars_after=80,
)
print(docx_file.data["filename"])

Create a zip file with fake data

from faker import Faker
from faker_file.providers.zip_file import ZipFileProvider
from faker_file.providers.helpers.inner import create_inner_zip_file, create_inner_docx_file
faker = Faker()
faker.add_provider(ZipFileProvider)
zip_file = faker.zip_file(
options={"create_inner_file_args": {"content": "Lorem ipsum"}}
)
print(zip_file.data["filename"])
zip_inner_file = faker.zip_file(
prefix="nested_level_0_",
options={
"create_inner_file_func": create_inner_zip_file,
"create_inner_file_args": {
"prefix": "nested_level_1_",
"options": {
"create_inner_file_func": create_inner_zip_file,
"create_inner_file_args": {
"prefix": "nested_level_2_",
"options": {
"count": 2,
"create_inner_file_func": create_inner_docx_file,
"create_inner_file_args": {
"content": "HELLO" + "\n\n{{date}}",
}
}
},
}
},
}
)
print(zip_inner_file.data["filename"])

Import the bin file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(BinFileProvider)) and call faker.bin_file().

from faker_file.providers.bin_file import BinFileProvider

Import the CSV file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(CsvFileProvider)) and call faker.csv_file().

from faker_file.providers.csv_file import CsvFileProvider

Import the EML file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(EmlFileProvider)) and call faker.eml_file().

from faker_file.providers.eml_file import EmlFileProvider

Import the EPUB file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(EpubFileProvider)) and call faker.epub_file().

from faker_file.providers.epub_file import EpubFileProvider

Import the ICO file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(IcoFileProvider)) and call faker.ico_file().

from faker_file.providers.ico_file import IcoFileProvider

Import the JPEG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(JpegFileProvider)) and call faker.jpeg_file().

from faker_file.providers.jpeg_file import JpegFileProvider

Import the MP3 file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(Mp3FileProvider)) and call faker.mp3_file().

from faker_file.providers.mp3_file import Mp3FileProvider

Import the ODS file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(OdsFileProvider)) and call faker.ods_file().

from faker_file.providers.ods_file import OdsFileProvider

Import the ODT file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(OdtFileProvider)) and call faker.odt_file().

from faker_file.providers.odt_file import OdtFileProvider

Import the PDF file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(PdfFileProvider)) and call faker.pdf_file().

from faker_file.providers.pdf_file import PdfFileProvider

Import the PNG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(PngFileProvider)) and call faker.png_file().

from faker_file.providers.png_file import PngFileProvider

Import the PPTX file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(PptxFileProvider)) and call faker.pptx_file().

from faker_file.providers.pptx_file import PptxFileProvider

Import the RTF file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(RtfFileProvider)) and call faker.rtf_file().

from faker_file.providers.rtf_file import RtfFileProvider

Import the SVG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(SvgFileProvider)) and call faker.svg_file().

from faker_file.providers.svg_file import SvgFileProvider

Import the WEBP file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(WebpFileProvider)) and call faker.webp_file().

from faker_file.providers.webp_file import WebpFileProvider

Import the XLSX file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(XlsxFileProvider)) and call faker.xlsx_file().

from faker_file.providers.xlsx_file import XlsxFileProvider

Example 20: GoogleCloudStorage (Google Cloud Files)

Section titled “Example 20: GoogleCloudStorage (Google Cloud Files)”

Import the Google Cloud Storage backend. Pass an instance as storage= to any provider method to write the file to a bucket instead of the local disk.

from faker_file.storages.google_cloud_storage import GoogleCloudStorage

Import the AWS S3 storage backend. Pass an instance as storage= to any provider method to write the file to a bucket instead of the local disk.

from faker_file.storages.aws_s3 import AWSS3Storage

Create a sftp file with fake data

import os
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.sftp_storage import SFTPStorage
SFTP_STORAGE = SFTPStorage(
host=os.environ["SFTP_HOST"],
port=int(os.environ.get("SFTP_PORT", "22")),
username=os.environ["SFTP_USERNAME"],
password=os.environ["SFTP_PASSWORD"],
root_path="/dir-name", # an existing directory on the server
)
faker = Faker()
faker.add_provider(TxtFileProvider)
txt_file = faker.txt_file(storage=SFTP_STORAGE)
print(txt_file.data["filename"])

Import the local file-system storage backend (the default storage of every provider). Pass an instance as storage= to choose where files are written.

from faker_file.storages.filesystem import FileSystemStorage

Import the generic file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GenericFileProvider)) and call faker.generic_file().

from faker_file.providers.generic_file import GenericFileProvider

Import the graphic ICO file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicIcoFileProvider)) and call faker.graphic_ico_file().

from faker_file.providers.ico_file import GraphicIcoFileProvider

Import the graphic JPEG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicJpegFileProvider)) and call faker.graphic_jpeg_file().

from faker_file.providers.jpeg_file import GraphicJpegFileProvider

Import the graphic PNG file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicPngFileProvider)) and call faker.graphic_png_file().

from faker_file.providers.png_file import GraphicPngFileProvider

Import the graphic WEBP file provider. To create a file, add it as in Examples 1-3 (faker.add_provider(GraphicWebpFileProvider)) and call faker.graphic_webp_file().

from faker_file.providers.webp_file import GraphicWebpFileProvider

Import the gTTS MP3 generator. Use it with faker.mp3_file(mp3_generator_cls=GttsMp3Generator).

from faker_file.providers.mp3_file.generators.gtts_generator import (
GttsMp3Generator,
)

Import the base class for writing your own MP3 generator.

from faker_file.providers.base.mp3_generator import BaseMp3Generator

Import the Edge TTS MP3 generator. Use it with faker.mp3_file(mp3_generator_cls=EdgeTtsMp3Generator).

from faker_file.providers.mp3_file.generators.edge_tts_generator import (
EdgeTtsMp3Generator,
)

Import the TextAugment-based text augmenter, which AugmentFileFromDirProvider can use to vary the text of existing files.

from faker_file.providers.augment_file_from_dir.augmenters import (
textaugment_augmenter,
)

Import the nlpaug-based text augmenter, which AugmentFileFromDirProvider can use to vary the text of existing files.

from faker_file.providers.augment_file_from_dir.augmenters import (
nlpaug_augmenter,
)

ganymede is made by Eugo