panda3d.core.Filename

class Filename

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

__init__() → None

Creates an empty Filename.

__init__(dirname: Filename, basename: Filename) → None

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

__init__(path: object) → None
static textFilename(filename: Filename) → Filename

Static constructors to explicitly create a filename that refers to a text or binary file. This is in lieu of calling setText() or setBinary() or setType().

Return type

Filename

static textFilename(filename: str) → Filename
Return type

Filename

static binaryFilename(filename: Filename) → Filename
Return type

Filename

static binaryFilename(filename: str) → Filename
Return type

Filename

static dsoFilename(filename: str) → Filename
Return type

Filename

static executableFilename(filename: str) → Filename
Return type

Filename

static patternFilename(filename: str) → Filename

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

Return type

Filename

static fromOsSpecific(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 toOsSpecific() 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.

Return type

Filename

static fromOsSpecificW(os_specific: str, type: Type) → Filename

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

Return type

Filename

static expandFrom(user_string: str, type: Type) → Filename

Returns the same thing as fromOsSpecific(), 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.

Return type

Filename

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.

Return type

Filename

static getHomeDirectory() → 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.

Return type

Filename

static getTempDirectory() → Filename

Returns a path to a system-defined temporary directory.

Return type

Filename

static getUserAppdataDirectory() → Filename

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

Return type

Filename

static getCommonAppdataDirectory() → Filename

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

Return type

Filename

assign(from: Filename) → Filename
Return type

Filename

assign(copy: Filename) → Filename
Return type

Filename

assign(filename: str) → Filename
Return type

Filename

assign(filename: str) → Filename

Assignment is via the = operator.

Return type

Filename

assign(filename: str) → Filename
Return type

Filename

cStr() → str
empty() → bool
length() → size_t
Return type

size_t

substr(begin: size_t) → str
substr(begin: size_t, end: size_t) → str
getFullpath() → str

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

getFullpathW() → str

Returns the entire filename as a wide-character string.

getDirname() → str

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

getBasename() → str

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

getFullpathWoExtension() → str

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

getBasenameWoExtension() → str

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

getExtension() → str

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

setFullpath(s: str) → None

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

setDirname(s: str) → None

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

setBasename(s: str) → None

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

setFullpathWoExtension(s: str) → None

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

setBasenameWoExtension(s: str) → None

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

setExtension(s: str) → None

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

setBinary() → None

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).

setText() → None

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.

isBinary() → bool

Returns true if the Filename has been indicated to represent a binary file via a previous call to setBinary(). It is possible that neither isBinary() nor isText() will be true, if neither setBinary() nor setText() was ever called.

isText() → bool

Returns true if the Filename has been indicated to represent a text file via a previous call to setText(). It is possible that neither isBinary() nor isText() will be true, if neither setBinary() nor setText() was ever called.

isBinaryOrText() → bool

Returns true either isBinary() or isText() 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.

setType(type: Type) → None

Sets the type of the file represented by the filename. This is useful for toOsSpecific(), resolveFilename(), 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).

getType() → Type

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

Return type

Type

setPattern(pattern: bool) → None

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 hasHash() and getHashToEnd() and getFilenameIndex() may be called. Methods like is_exists() will implicitly test for existance of filename sequence 0.

getPattern() → bool

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

hasHash() → 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 getFilenameIndex() will return a different filename each time.

getFilenameIndex(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.

Return type

Filename

getHashToEnd() → str

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

setHashToEnd(s: str) → None

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

extractComponents(components: vector_string) → None

Extracts out the individual directory components of the path into a series of strings. getBasename() 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 isLocal() to differentiate the two cases.

standardize() → None

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.

isLocal() → 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.

isFullyQualified() → 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 !:meth:~Filename.isLocal. 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).

makeAbsolute() → None

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.

makeAbsolute(start_directory: Filename) → None

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.

makeCanonical() → 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 makeAbsolute() 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 makeRelativeTo() 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).

makeTrueCase() → 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.

toOsSpecific() → 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 fromOsSpecific().

toOsSpecificW() → str

The wide-string variant on toOsSpecific().

toOsGeneric() → str

This is similar to toOsSpecific(), 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 toOsSpecific(), 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.

toOsShortName() → str

This works like toOsGeneric(), 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 toOsSpecific() otherwise. On non-Windows platforms, this always returns the same thing as toOsSpecific().

toOsLongName() → str

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

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.

isRegularFile() → bool

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

isWritable() → bool

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

isDirectory() → bool

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

isExecutable() → bool

Returns true if the filename exists and is executable

compareTimestamps(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.

getTimestamp() → time_t

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.

Return type

time_t

getAccessTimestamp() → time_t

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

Return type

time_t

getFileSize() → streamsize

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

Return type

streamsize

resolveFilename(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.

makeRelativeTo(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 makeCanonical() on both filenames before calling makeRelativeTo().

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.

findOnSearchpath(searchpath: DSearchPath) → int

Performs the reverse of the resolveFilename() 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.

scanDirectory() → object
scanDirectory(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.

openRead(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

openRead(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

openWrite(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

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.

openWrite(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

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.

openAppend(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

openAppend(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

openReadWrite(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openReadWrite() without first calling one of setText() or setBinary().

openReadWrite(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openReadWrite() without first calling one of setText() or setBinary().

openReadAppend(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

openReadAppend(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 setText()/setBinary() flags to open the file appropriately as indicated; it is an error to call openRead() without first calling one of setText() or setBinary().

chdir() → bool

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

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).

renameTo(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.

copyTo(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.

makeDir() → 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.

mkdir() → bool

Creates the directory named by this filename. Unlike makeDir(), 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.

rmdir() → bool

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

compareTo(other: Filename) → int
getHash() → int

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

output(out: ostream) → None
static setFilesystemEncoding(encoding: Encoding) → None

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.

static getFilesystemEncoding() → Encoding

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

Return type

Encoding

static getClassType() → TypeHandle
Return type

TypeHandle

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