在操作路径和文件名时,我总是陷入困境,因为我没有遵循路径组件的命名标准。

考虑下面的玩具问题(以Windows为例,但希望答案与平台无关)。你已经得到了一个文件夹的路径:

C:\Users\OddThinking\Documents\My Source\

您希望遍历下面的文件夹并将所有.src文件编译为.obj文件。

在某种程度上,你会看到以下路径:

C:\Users\OddThinking\Documents\My Source\Widget\foo.src

你如何命名下面的路径组件?

A. foo
B. foo.src
C. src
D. .src
E. C:\Users\OddThinking\Documents\My Source\ (absolute path of the root)
F. Widget\foo.src (relative path of the file to absolute path of the root)
G. Widget\
H. C:\Users\OddThinking\Documents\My Source\Widget\
I. C:\Users\OddThinking\Documents\My Source\Widget\foo.src

以下是我的尝试:

A.基地名称?Basename吗?

B.文件名?文件名吗?在选择标识符名称时,差异很重要,这里我从来没有保持一致。

c扩展吗?

d .扩展吗?等等,这就是我所说的c,我应该避免存储点,只在需要的时候放进去吗?如果某个文件上没有点怎么办?

E. ?

F. ?

g .文件夹吗?但这不是windows特有的术语吗?

H.路径名称?路径名吗?路径?

一、文件名?等等,这就是我说的c路径名?等等,这就是我说的H。


当前回答

Python中的Pathlib标准库遵循路径组件的命名约定:

A. /x/y/z.tar.gz: stem。

B. /x/y/z.tar.gz:名称。

C. /x/y/z.tar.gz(不包括圆点):N/A。

D. /x/y/z.tar.gz(包括点):后缀。

E. /x/y/z.tar.gz:父路径。

F. /x/y/z.tar.gz:父路径的相对路径。

G. /x/y/z.tar.gz:父名称。

H. /x/y/z.tar.gz:父路径。

I. /x/y/z.tar.gz:路径。

其他回答

foo Filename Without Extension foo.src Filename src Extension .src Maybe Extension With Dot, but this should not be used. As written this could be a directory name or a filename. C:\users\OddThinking\Documents\My Source\ [Absolute] Directory Path Widget\foo.src Relative File Path Widget Directory Name C:\users\OddThinking\Documents\My Source\Widget\ This is still an Absolute Directory Path. If one is root and the other isn't, its up to your variable names to keep track of that, there isn't really a semantic difference there. C:\users\OddThinking\Documents\My Source\Widget\foo.src [Absolute] File Path

“Filename”是一个单词,所以通常我们应该使用“Filename”而不是“文件名”(以及Filename而不是Filename)。

“目录”可以替换为“文件夹”。(也许我们应该在较低的层次上使用“目录”,但“文件夹”更短,这是我更喜欢的。)

实际上可以创建所有这些类型的语义框架,并使用语义上有效的函数来组合它们。例如,FolderName和Filename可以组合成RelativeFilePath。一个FolderPath(绝对路径)和一个RelativeFilePath可以组合成一个FilePath(绝对路径)。

还有一些是相关的;例如,FilenameWithoutExtension是一种文件名,所以应该是可转换的。FolderName是相对文件夹路径,所以应该是可转换的。等。

对于简单的项目,你可以采用简单的答案: 没有使用过路径这个词,因为如果使用url /path,它有助于将其与web路径区分开来。

| name       | example          |
|------------|------------------|
| file       | /foo/bar/baa.txt |
| filename   | baa.txt          |
| stem       | baa              |
| suffix     | .txt             |
| ext        | txt              |
| dir        | /foo/bar/        |
| dirname    | bar              |
| parent     | /foo/bar/        |
| parentname | bar              |

Python中的Pathlib标准库遵循路径组件的命名约定:

A. /x/y/z.tar.gz: stem。

B. /x/y/z.tar.gz:名称。

C. /x/y/z.tar.gz(不包括圆点):N/A。

D. /x/y/z.tar.gz(包括点):后缀。

E. /x/y/z.tar.gz:父路径。

F. /x/y/z.tar.gz:父路径的相对路径。

G. /x/y/z.tar.gz:父名称。

H. /x/y/z.tar.gz:父路径。

I. /x/y/z.tar.gz:路径。

不,你没疯。

在Windows系统中,有时包含文件的目录的路径被称为path,从一开始就是这样。举个例子,

    x:\dir1\dir2\myfile.txt

    Windows:
    --------
        PATH:  x:\dir1\dir2
        FILE:  myfile.txt

    Unix/Linux:
    -----------
        PATH:  /dir1/dir2/myfile.txt
        FILE:  myfile.txt

Unix/Linux方法更符合逻辑,这就是上面每个人都提到的:路径包括文件名本身。然而,如果你在Windows命令行中输入“call /?”,你会得到这样的结果:

    %~1         - expands %1 removing any surrounding quotes (")
    %~f1        - expands %1 to a fully qualified path name
    %~d1        - expands %1 to a drive letter only
    %~p1        - expands %1 to a path only
    %~n1        - expands %1 to a file name only
    %~x1        - expands %1 to a file extension only

这就是,“仅路径”和“仅文件名”。同时,它们将整个字符串称为“完全限定路径名”,即驱动器号加路径加文件名。所以没有真正的真相。这是徒劳的。你被背叛了。

不管怎么说,

回答你的问题

以下是我给你举的例子:

A: -
B: basename
C: extension
D: -
E: -
F: -
G: -
H: pathname (or dirname or containing path)
I: full name

A-D-E-F没有简单的昵称。由于php可能是最广为人知的跨平台语言,每个人都理解“basename”和“dirname”,所以我坚持使用这个命名。全名也很明显;全路径可能有点模糊,但大多数时候它的意思是完全相同的。

在我花了10年的钱之后

是一致的 认为递归

Windows机器上的示例:

File separator: \ 
Line separator: 

Base name: file 
Extension: txt 
Filename: file.txt     

Drive name: C 
Root name: C: (empty on linux) 
Root dir: \ 
Root path: C:\     

Base dir: Source\ 
Base path: C:\Source\ 
Sub dir: project\ 
Sub-sub dir: docs\ 
Relative dir: project\docs\     

Relative path: project\docs\file.txt 
Working dir: C:\Source\project\docs\
Full path: C:\Source\project\docs\file.txt (also 'Absolute path' or 'File path')   

Linux drive dir: C\ 
Linux root path: \C\ 
Linux base path: \C\Source\     

Parent dir: ..\ 
Current dir: .\

接近底部的linux内容是bash如何在Windows系统上安装驱动器。

当前目录,或工作“目录”,实际上是您的程序所在的位置,但是让我们使用它来跟踪我们正在处理的当前文件的位置。在powershell中输入pwd,结果被称为路径!

目录总是以文件分隔符结尾,从不包含文件名。它们很容易被追加。 “目录名”可以指任意位置的任意目录(dirName + sep = dir)。

路径包括根文件、文件名或两者都包含。

也就是说,路径可以通过向目录中添加根或文件名或两者之一来形成。(你可以区分路径和文件路径,“相对路径”会排除文件名,但会给出从基本目录到工作目录的目录,尽管这个术语变得多余,因为它被正确地称为相对目录)。

注意关键字的不同含义:

的名字 目录 路径 分隔符

然后将它们与完整路径的部分结合起来:

根 基地 相对 文件


示例:根路径=根名称+根目录

注意这对于Windows和Linux是如何工作的(根路径与根目录相同,因为根名称是空的)。


在Java中,输出由:

package io;

import java.io.File;
import java.util.logging.Logger;

/**
 * Directory, File, and Path conventions.
 *
 * Directories always end with the file separator and never include the filename. They can easily be appended.
 * - "Directory name" could refer to any directory in any position (dirName + sep = dir).
 *
 * Paths include the root, the filename, or both.
 *
 * <em>On Windows, base directory names can be capitalised.</em>
 */
public class Main {
    private static Logger logger = Logger.getLogger("io");

    public static void main(String[] args) {
        final String sep = File.separator;
        final String lf = System.lineSeparator();

        logger.info("File separator: " + sep);
        logger.info("Line separator: " + lf);

        String baseName = "file";
        String ext = "txt";
        String fileName = baseName + "." + ext;

        String driveName = "C";

        String rootName = driveName + ":";
        String rootDir = sep;
        String rootPath = rootName + rootDir;

        String baseDir = "Source" + sep;
        String basePath = rootPath + baseDir;

        String subDir = "project" + sep;
        String subSubDir = "docs" + sep;
        String relDir = subDir + subSubDir;

        String relPath = relDir + fileName;
        String workDir = basePath + relDir;
        String fullPath = basePath + relPath;

        logger.info("Base name: " + baseName);
        logger.info("Extension: " + ext);
        logger.info("Filename: " + fileName);
        logger.info(lf);

        logger.info("Drive name: " + driveName);
        logger.info("Root name: " + rootName + " (empty on linux)");
        logger.info("Root dir: " + rootDir);
        logger.info("Root path: " + rootPath);
        logger.info(lf);

        logger.info("Base dir: " + baseDir);
        logger.info("Base path: " + basePath);
        logger.info("Sub dir: " + subDir);
        logger.info("Sub-sub dir: " + subSubDir);
        logger.info("Relative dir: " + relDir);
        logger.info(lf);

        logger.info("Relative path: " + relPath);
        logger.info("Working dir: " + workDir);
        logger.info("Full path: " + fullPath + " (also 'Absolute path' or 'File path')");

        logger.info(lf);
        String linuxDriveDir = driveName + sep;
        String linuxRootPath = rootDir + linuxDriveDir;
        String linuxBasePath = linuxRootPath + baseDir;
        logger.info("Linux drive dir: " + linuxDriveDir);
        logger.info("Linux root path: " + linuxRootPath);
        logger.info("Linux base path: " + linuxBasePath);

        logger.info(lf);
        String parentDir = ".." + sep;
        String currDir = "." + sep;
        logger.info("Parent dir: " + parentDir);
        logger.info("Current dir: " + currDir);
    }
}

回答OP的问题:

A) foo = base name
B) foo.src = file name
C) src = extension
D) .src = ? (file extension separator + extension)
E) C:\users\OddThinking\Documents\My Source\ = base path
F) Widget\foo.src = relative (file) path
G) Widget = directory name
H) C:\users\OddThinking\Documents\My Source\Widget\ = working path aka "working directory"
I) C:\users\OddThinking\Documents\My Source\Widget\foo.src = full path, absolute path, file path