Filename

from panda3d.core import Filename
class Filename

Bases:

The name of a file, such as a texture file or an Egg file. Stores the full pathname, and includes functions for extracting out the directory prefix part and the file extension and stuff.

A Filename is also aware of the mapping between the Unix-like filename convention we use internally, and the local OS’s specific filename convention, and it knows how to perform basic OS-specific I/O, like testing for file existence and searching a searchpath, as well as the best way to open an fstream for reading or writing.

Inheritance diagram

Inheritance diagram of Filename

enum Type
enumerator T_general = 0

These type values must fit within the bits allocated for F_type, below.

enumerator T_dso = 1
enumerator T_executable = 2
__add__(other: str) Filename
__bool__() bool

Returns true if the Filename is valid (not empty), or false if it is an empty string.

This implements the Python equivalent to operator bool. Defining an actual operator bool method for C++ use would work too, but it seems to cause too many ambiguities for the C++ compiler, so we use this Python-only approach instead.

__div__(other: Filename) Filename
__eq__(other: str) bool

Comparison operators are handy.

__fspath__() object
__getitem__(n: int) char
__iadd__(other: str) Filename
__init__()

Creates an empty Filename.

__init__(dirname: Filename, basename: Filename)

This constructor composes the filename out of a directory part and a basename part. It will insert an intervening ‘/’ if necessary.

__init__(path: object)
__lt__(other: str) bool
__ne__(other: str) bool
__reduce__() object
__repr__() object
assign(copy: Filename) Filename
assign(filename: str) Filename
assign(filename: str) Filename

Assignment is via the = operator.

assign(filename: str) Filename
static binary_filename(filename: Filename) Filename
static binary_filename(filename: str) Filename
c_str() str
chdir() bool

Changes directory to the specified location. Returns true if successful, false if failure.

compare_timestamps(other: Filename, this_missing_is_old: bool, other_missing_is_old: bool) int

Returns a number less than zero if the file named by this object is older than the given file, zero if they have the same timestamp, or greater than zero if this one is newer.

If this_missing_is_old is true, it indicates that a missing file will be treated as if it were older than any other file; otherwise, a missing file will be treated as if it were newer than any other file. Similarly for other_missing_is_old.

compare_to(other: Filename) int
copy_to(other: Filename) bool

Copies the file to the indicated new filename, by reading the contents and writing it to the new file. Returns true if successful, false on failure. The copy is always binary, regardless of the filename settings.

static dso_filename(filename: str) Filename
empty() bool
static executable_filename(filename: str) Filename
exists() bool

Returns true if the filename exists on the disk, false otherwise. If the type is indicated to be executable, this also tests that the file has execute permission.

static expand_from(user_string: str, type: Type) Filename

Returns the same thing as from_os_specific(), but embedded environment variable references (e.g. “$DMODELS/foo.txt”) are expanded out. It also automatically elevates the file to its true case if needed.

extract_components(components: vector_string)

Extracts out the individual directory components of the path into a series of strings. get_basename() will be the last component stored in the vector. Note that no distinction is made by this method between a leading slash and no leading slash, but you can call is_local() to differentiate the two cases.

find_on_searchpath(searchpath: DSearchPath) int

Performs the reverse of the resolve_filename() operation: assuming that the current filename is fully-specified pathname (i.e. beginning with ‘/’), look on the indicated search path for a directory under which the file can be found. When found, adjust the Filename to be relative to the indicated directory name.

Returns the index of the directory on the searchpath at which the file was found, or -1 if it was not found.

static from_os_specific(os_specific: str, type: Type) Filename

This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).

Use this function to create a Filename from an externally-given filename string. Use to_os_specific() again later to reconvert it back to the local operating system’s conventions.

This function will do the right thing even if the filename is partially local conventions and partially Panda conventions; e.g. some backslashes and some forward slashes.

static from_os_specific_w(os_specific: str, type: Type) Filename

The wide-string variant of from_os_specific(). Returns a new Filename, converted from an os-specific wide-character string.

get_access_timestamp() int

Returns a time_t value that represents the time the file was last accessed, if this information is available. See also get_timestamp(), which returns the last modification time.

get_basename() str

Returns the basename part of the filename. This is everything in the filename after the rightmost slash, including any extensions.

get_basename_wo_extension() str

Returns the basename part of the filename, without the file extension.

static get_class_type() TypeHandle
static get_common_appdata_directory() Filename

Returns a path to a system-defined directory appropriate for creating a subdirectory for storing application-specific data, common to all users.

get_dirname() str

Returns the directory part of the filename. This is everything in the filename up to, but not including the rightmost slash.

get_extension() str

Returns the file extension. This is everything after the rightmost dot, if there is one, or the empty string if there is not.

get_file_size() int

Returns the size of the file in bytes, or 0 if there is an error.

get_filename_index(index: int) Filename

If the pattern flag is set for this Filename and the filename string actually includes a sequence of hash marks, then this returns a new Filename with the sequence of hash marks replaced by the indicated index number.

If the pattern flag is not set for this Filename or it does not contain a sequence of hash marks, this quietly returns the original filename.

static get_filesystem_encoding() Encoding

Specifies the default encoding to be used for all subsequent Filenames objects. See set_filesystem_encoding().

get_fullpath() str

Returns the entire filename: directory, basename, extension. This is the same thing returned by the string typecast operator.

get_fullpath_w() str

Returns the entire filename as a wide-character string.

get_fullpath_wo_extension() str

Returns the full filename–directory and basename parts–except for the extension.

get_hash() int

Returns a hash code that attempts to be mostly unique for different Filenames.

get_hash_to_end() str

Returns the part of the filename beginning at the hash sequence (if any), and continuing to the end of the filename.

static get_home_directory() Filename

Returns a path to the user’s home directory, if such a thing makes sense in the current OS, or to the nearest equivalent. This may or may not be directly writable by the application.

get_pattern() bool

Returns the flag indicating whether this is a filename pattern. See set_pattern().

static get_temp_directory() Filename

Returns a path to a system-defined temporary directory.

get_timestamp() int

Returns a time_t value that represents the time the file was last modified, to within whatever precision the operating system records this information (on a Windows95 system, for instance, this may only be accurate to within 2 seconds).

If the timestamp cannot be determined, either because it is not supported by the operating system or because there is some error (such as file not found), returns 0.

get_type() Type

Returns the type of the file represented by the filename, as previously set by set_type().

static get_user_appdata_directory() Filename

Returns a path to a system-defined directory appropriate for creating a subdirectory for storing application-specific data, specific to the current user.

has_hash() bool

Returns true if the filename is indicated to be a filename pattern (that is, set_pattern(true) was called), and the filename pattern did include a sequence of hash marks, or false if it was not a filename pattern or did not include hash marks. If this is true, then get_filename_index() will return a different filename each time.

is_binary() bool

Returns true if the Filename has been indicated to represent a binary file via a previous call to set_binary(). It is possible that neither is_binary() nor is_text() will be true, if neither set_binary() nor set_text() was ever called.

is_binary_or_text() bool

Returns true either is_binary() or is_text() is true; that is, that the filename has been specified as either binary or text. If this is false, the filename has not been specified.

is_directory() bool

Returns true if the filename exists and is a directory name, false otherwise.

is_executable() bool

Returns true if the filename exists and is executable

is_fully_qualified() bool

Returns true if the filename is fully qualified, e.g. begins with a slash. This is almost, but not quite, the same thing as !is_local(). It’s not exactly the same because a special case is made for filenames that begin with a single dot followed by a slash–these are considered to be fully qualified (they are explicitly relative to the current directory, and do not refer to a filename on a search path somewhere).

is_local() bool

Returns true if the filename is local, e.g. does not begin with a slash, or false if the filename is fully specified from the root.

is_regular_file() bool

Returns true if the filename exists and is the name of a regular file (i.e. not a directory or device), false otherwise.

is_text() bool

Returns true if the Filename has been indicated to represent a text file via a previous call to set_text(). It is possible that neither is_binary() nor is_text() will be true, if neither set_binary() nor set_text() was ever called.

is_writable() bool

Returns true if the filename exists and is either a directory or a regular file that can be written to, or false otherwise.

length() int
make_absolute()

Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname), and then standardizes it (see standardize()).

This is sometimes a little problematic, since it may convert the file to its ‘true’ absolute pathname, which could be an ugly NFS-named file, irrespective of symbolic links (e.g. /.automount/dimbo/root/usr2/fit/people/drose instead of /fit/people/drose); besides being ugly, filenames like this may not be consistent across multiple different platforms.

make_absolute(start_directory: Filename)

Converts the filename to a fully-qualified filename from the root (if it is a relative filename), and then standardizes it (see standardize()). This flavor accepts a specific starting directory that the filename is known to be relative to.

make_canonical() bool

Converts this filename to a canonical name by replacing the directory part with the fully-qualified directory part. This is done by changing to that directory and calling getcwd().

This has the effect of (a) converting relative paths to absolute paths (but see make_absolute() if this is the only effect you want), and (b) always resolving a given directory name to the same string, even if different symbolic links are traversed, and (c) changing nice symbolic-link paths like fit/people/drose to ugly NFS automounter names like hosts/dimbo/usr2/fit/people/drose. This can be troubling, but sometimes this is exactly what you want, particularly if you’re about to call make_relative_to() between two filenames.

The return value is true if successful, or false on failure (usually because the directory name does not exist or cannot be chdir’ed into).

make_dir() bool

Creates all the directories in the path to the file specified in the filename, except for the basename itself. This assumes that the Filename contains the name of a file, not a directory name; it ensures that the directory containing the file exists.

However, if the filename ends in a slash, it assumes the Filename represents the name of a directory, and creates all the paths.

make_relative_to(directory: Filename, allow_backups: bool) bool

Adjusts this filename, which must be a fully-specified pathname beginning with a slash, to make it a relative filename, relative to the fully- specified directory indicated (which must also begin with, and may or may not end with, a slash–a terminating slash is ignored).

This only performs a string comparsion, so it may be wise to call make_canonical() on both filenames before calling make_relative_to().

If allow_backups is false, the filename will only be adjusted to be made relative if it is already somewhere within or below the indicated directory. If allow_backups is true, it will be adjusted in all cases, even if this requires putting a series of .. characters before the filename –unless it would have to back all the way up to the root.

Returns true if the file was adjusted, false if it was not.

make_true_case() bool

On a case-insensitive operating system (e.g. Windows), this method looks up the file in the file system and resets the Filename to represent the actual case of the file as it exists on the disk. The return value is true if the file exists and the conversion can be made, or false if there is some error.

On a case-sensitive operating system, this method does nothing and always returns true.

An empty filename is considered to exist in this case.

mkdir() bool

Creates the directory named by this filename. Unlike make_dir(), this assumes that the Filename contains the directory name itself. Also, parent directories are not automatically created; this function fails if any parent directory is missing.

open_append(stream: pofstream) bool

Opens the indicated pifstream for writing the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

open_append(stream: ofstream) bool

Opens the indicated ofstream for writing the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

open_read(stream: pifstream) bool

Opens the indicated pifstream for reading the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

open_read(stream: ifstream) bool

Opens the indicated ifstream for reading the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

open_read_append(stream: pfstream) bool

Opens the indicated pfstream for reading and writing the file, if possible; writes are appended to the end of the file. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

open_read_append(stream: fstream) bool

Opens the indicated ifstream for reading and writing the file, if possible; writes are appended to the end of the file. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

open_read_write(stream: pfstream, truncate: bool) bool

Opens the indicated fstream for read/write access to the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read_write() without first calling one of set_text() or set_binary().

open_read_write(stream: fstream, truncate: bool) bool

Opens the indicated fstream for read/write access to the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read_write() without first calling one of set_text() or set_binary().

open_write(stream: pofstream, truncate: bool) bool

Opens the indicated pifstream for writing the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

If truncate is true, the file is truncated to zero length upon opening it, if it already exists. Otherwise, the file is kept at its original length.

open_write(stream: ofstream, truncate: bool) bool

Opens the indicated ifstream for writing the file, if possible. Returns true if successful, false otherwise. This requires the setting of the set_text()/set_binary() flags to open the file appropriately as indicated; it is an error to call open_read() without first calling one of set_text() or set_binary().

If truncate is true, the file is truncated to zero length upon opening it, if it already exists. Otherwise, the file is kept at its original length.

output(out: ostream)
static pattern_filename(filename: str) Filename

Constructs a filename that represents a sequence of numbered files. See set_pattern().

rename_to(other: Filename) bool

Renames the file to the indicated new filename. If the new filename is in a different directory, this will perform a move. Returns true if successful, false on failure.

resolve_filename(searchpath: DSearchPath, default_extension: str) bool

Searches the given search path for the filename. If it is found, updates the filename to the full pathname found and returns true; otherwise, returns false.

rmdir() bool

The inverse of mkdir(): this removes the directory named by this Filename, if it is in fact a directory.

scan_directory() object
scan_directory(contents: vector_string) bool

Attempts to open the named filename as if it were a directory and looks for the non-hidden files within the directory. Fills the given vector up with the sorted list of filenames that are local to this directory.

It is the user’s responsibility to ensure that the contents vector is empty before making this call; otherwise, the new files will be appended to it.

Returns true on success, false if the directory could not be read for some reason.

set_basename(s: str)

Replaces the basename part of the filename. This is everything in the filename after the rightmost slash, including any extensions.

set_basename_wo_extension(s: str)

Replaces the basename part of the filename, without the file extension.

set_binary()

Setting these flags appropriately is helpful when opening or searching for a file; it helps the Filename resolve OS-specific conventions (for instance, that dynamic library names should perhaps be changed from .so to .dll).

set_dirname(s: str)

Replaces the directory part of the filename. This is everything in the filename up to, but not including the rightmost slash.

set_extension(s: str)

Replaces the file extension. This is everything after the rightmost dot, if there is one, or the empty string if there is not.

static set_filesystem_encoding(encoding: Encoding)

Specifies the default encoding to be used for all subsequent Filenames. This is used to represent wide-character (Unicode) filenames internally. On non-Windows-based systems, the encoded filename is also passed to the underlying operating system.

set_fullpath(s: str)

Replaces the entire filename: directory, basename, extension. This can also be achieved with the assignment operator.

set_fullpath_wo_extension(s: str)

Replaces the full filename–directory and basename parts–except for the extension.

set_hash_to_end(s: str)

Replaces the part of the filename from the beginning of the hash sequence to the end of the filename.

set_pattern(pattern: bool)

Sets the flag indicating whether this is a filename pattern. When this is true, the filename is understood to be a placeholder for a numbered sequence of filename, such as an image sequence. In this case, a sequence of one or more hash characters (“#”) should appear in the filename string; these characters will be filled in with the corresponding number (or more) of digits representing the sequence number. Sequence numbers always begin counting at 0.

When this is true, methods like has_hash() and get_hash_to_end() and get_filename_index() may be called. Methods like is_exists() will implicitly test for existance of filename sequence 0.

set_text()

Indicates that the filename represents a text file. This is primarily relevant to the read_file() and write_file() methods, so they can set the appropriate flags to the OS.

set_type(type: Type)

Sets the type of the file represented by the filename. This is useful for to_os_specific(), resolve_filename(), test_existence(), and all such real- world access functions. It helps the Filename know how to map the internal filename to the OS-specific filename (for instance, maybe executables should have an .exe extension).

standardize()

Converts the filename to standard form by replacing consecutive slashes with a single slash, removing a trailing slash if present, and backing up over .. sequences within the filename where possible.

substr(begin: int) str
substr(begin: int, end: int) str
static temporary(dirname: str, prefix: str, suffix: str, type: Type) Filename

Generates a temporary filename within the indicated directory, using the indicated prefix. If the directory is empty, a system-defined directory is chosen instead.

The generated filename did not exist when the Filename checked, but since it does not specifically create the file, it is possible that another process could simultaneously create a file by the same name.

static text_filename(filename: Filename) Filename

Static constructors to explicitly create a filename that refers to a text or binary file. This is in lieu of calling set_text() or set_binary() or set_type().

static text_filename(filename: str) Filename
to_os_generic() str

This is similar to to_os_specific(), but it is designed to generate a filename that can be understood on as many platforms as possible. Since Windows can usually understand a forward-slash-delimited filename, this means it does the same thing as to_os_specific(), but it uses forward slashes instead of backslashes.

This method has a pretty limited use; it should generally be used for writing file references to a file that might be read on any operating system.

to_os_long_name() str

This is the opposite of to_os_short_name(): it returns the “long name” of the filename, if the filename exists. On non-Windows platforms, this returns the same thing as to_os_specific().

to_os_short_name() str

This works like to_os_generic(), but it returns the “short name” version of the filename, if it exists, or the original filename otherwise.

On Windows platforms, this returns the 8.3 filename version of the given filename, if the file exists, and the same thing as to_os_specific() otherwise. On non-Windows platforms, this always returns the same thing as to_os_specific().

to_os_specific() str

Converts the filename from our generic Unix-like convention (forward slashes starting with the root at ‘/’) to the corresponding filename in the local operating system (slashes in the appropriate direction, starting with the root at C:, for instance). Returns the string representing the converted filename, but does not change the Filename itself.

See also from_os_specific().

to_os_specific_w() str

The wide-string variant on to_os_specific().

touch() bool

Updates the modification time of the file to the current time. If the file does not already exist, it will be created. Returns true if successful, false if there is an error.

Permanently deletes the file associated with the filename, if possible. Returns true if successful, false if failure (for instance, because the file did not exist, or because permissions were inadequate).