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

考虑下面的玩具问题(以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。


当前回答

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              |

我认为您对“标准”命名约定的搜索将是徒劳的。以下是我根据现有的知名项目提出的建议:

A) C:\users\OddThinking\Documents\My Source\Widget\foo.src
                                                   ---

Vim称其为文件根(:help filename-modifiers)

B) C:\users\OddThinking\Documents\My Source\Widget\foo.src
                                                   -------

文件名或基本名称

C) C:\users\OddThinking\Documents\My Source\Widget\foo.src
                                                       ___ (without dot)

-文件name分机

D) C:\users\OddThinking\Documents\My Source\Widget\foo.src
                                                      ____ (with dot)

还有文件扩展名。简单地存储没有点,如果文件上没有点,它就没有扩展名

E) C:\users\OddThinking\Documents\My Source\Widget\foo.src
   -----------------------------------------

树的顶端 没有约定,git称它为base directory

F) C:\users\OddThinking\Documents\My Source\Widget\foo.src
                                            --------------

从树顶到树叶的路径 相对路径

G) C:\users\OddThinking\Documents\My Source\Widget\foo.src
                                            ------

树的一个节点 没有约定,可能是一个简单的目录

H) C:\users\OddThinking\Documents\My Source\Widget\foo.src
   ------------------------------------------------

你的名字

I) C:\users\OddThinking\Documents\My Source\Widget\foo.src
   -------------------------------------------------------

全/绝对路径

不,你没疯。

在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

首先,好问题,我的+1。当我必须在Utility类中创建大量函数时,这件事让我很困扰。GetFileName吗?还是GetFullName ?GetApplicationPath是指完整路径还是目录名?等等。我来自。net背景,所以我认为我可以为@blinry的出色回答补充更多。

总结:(斜体字是我作为程序员不会使用的内容)

Path: Path specifies a unique location in the file system (unless its relative path). Path name is less often used, but I would stick with path - it pretty much explains what it is. Path can point to a file or a folder or even nothing (C:\). Path can be: Relative Path: My Source\Widget\ is relative path as well as Widget\foo.src. Self explanatory. Absolute Path or Full Path: Is the fully qualified path that points to the target. I tend to use the latter more often. C:\users\OddThinking\Documents\My Source\Widget\foo.src is hence full path. See at the end what I call full path that points to a file and that ends as a directory. The wiki page and .NET naming for path is consistent. Root Path or Root Directory: Former is .NET convention while latter is more heard in UNIX circles. Though I like both I tend to use the former more. In windows, unlike UNIX, has many different root paths, one for each partition. Unix systems have one root directory which holds information on other directories and files. Eg. C:\ is root path. Folder or Folder Name: Widget, OddThinking etc in your case. This might be a Windows only convention (in fact its my own odd thinking :)), nevertheless I strongly object to blinry`s answer "Directory". Though for a normal user directory means the same as a folder (like subfolders, subdirectories), I believe from a technical angle "directory" should sound like a qualified address to the target and not the target itself. More below. Sub Folders: With respect to users OddThinking and Documents are sub folders. Sub Directories: With respect to users OddThinking\, OddThinking\Documents\ and OddThinking\Documents\My Source\Widget\ are sub directories. But we do not often need to bother about it, do we? Child Folder: With respect to users OddThinking is a child folder (as well as sub folder) Parent Folder: For OddThinking users is its parent folder (Just mentioning different terminologies, no big deal). Directory or Directory Name: The former to use generally in real life, the latter to be in code. This refers to the fully qualified path (or simply full path) till the target's parent folder. In your case, C:\users\OddThinking\Documents\My Source\Widget (Yes a directory is never meant to point to a file). I use directory name in my code since directory is a class in .NET and Directory Name is what the library itself calls it. Its quite consistent with dirname used in UNIX systems. File Name or Basename: Name of the file along with extension. In your case: foo.src. I would say that for a non technical use I prefer file name (it is what it means for an end user) but for technical purposes I would strictly stick with basename. File Name is often used by MS, but I am surprised how they are not consistent not just in documentation but even in library. There filename could mean either basename or full path of the file. So I favour basename, that's what I call them in code. This page on wiki too says file name could mean either full path or the basename. Surprisingly even in .NET I can find the usage basename to mean the root name of the file. Extension or Filename Extension or File Extension: I like the last one. All refers to the same thing but what is it is again a matter of debate! Wiki says it is src while back then I remember reading that many of the languages interprets it as .src. Note the dot. So once again my take is, for casual uses it doesn't matter what it is, but as a programmer I always see extension as .src. Ok I might have tried to fetch some standard usages, but here are two of my conventions I follow. And it is about full paths. I generally call a full path that point to a file as file path. To me file path is clear cut, it tells me what it is. Though with file name I find it as the name of the file, in my code I call it file name. It's also consistent with "directory name". From the technical side, name refers to the fully qualified name! Frustratingly .NET uses the term file name (so I have my case here) and sometimes file path for this. I call a full path that ends as a directory a directory. In fact one can call any piece of address that doesn't point to a file a directory. So C:\users\OddThinking\Documents\My Source\ is a directory, C:\users\OddThinking\ is a directory, or even OddThinking\Documents\My Source\ (better to call it sub directory or even better relative path - all that depends on the context you are dealing with it). Well above I mentioned something different about directory which is directory name. Here is my take on it: I'll get a new path to avoid confusion. What is this D:\Fruit\Apple\Pip\? A directory. But if the question is what is the directory or even better directory name of D:\Fruit\Apple\Pip\, the answer is D:\Fruit\Apple\. Hope its clear. I would say it's better not to worry about the final two terms as that is what create the most confusion (for me personally). Just use the term full path!

回答你:

with respect to the path you have given A) No idea. Anyways I never needed to get that one alone. B) basename C) I would just call it file extension for time being, I am least worried since I never needed that alone to be named in my code. D) file extension surely. E) I do not think this is a general purpose requirement. No idea. In .NET base directory is the same as directory name. F) relative path G) folder (parent folder to basename foo.src) H) directory name I) full path (or even file name) in general (sorry for being a bit verbose, just to drive the point home) but assuming foo.src is indeed a file A) NA B) basename C) NA D) extension E) directory or simply path F) relative path G) NA H) directory or simply path I) full path (or even file name)

举一个我的例子:

考虑路径C:\Documents and Settings\All Users\Application Data\s.sql。 C:\Documents and Settings\All用户\应用程序数据。SQL是完整路径(是一个文件名) C:\Documents and Settings\All Users\Application Data\是目录名。 现在考虑路径C:\Documents and Settings\All Users\Application Data C:\Documents and Settings\All Users\Application Data是完整路径(恰好是一个目录) “C:\Documents and Settings\All Users”为目录名。

我的两点建议:

I follow this rule of thumb that when it comes to addressing a full address irrespective of its type, I almost always call it "full path". This not only eliminates the use of two terminologies for file path and folder path, it also avoids the potential confusion if you are going to name that of file as file name (which for most users right away translates to basename). But yes if you have to be specific about the type of path, its better to name then file name or directory instead of more generic "path". Whatever it is you would have your own idea in mind, be consistent with it throughout. Have a consensus among team members that this means this and not that.

现在我有了一些练习。在OS X和android机器上使用的术语将会是一个新的品牌。所有这些都是关于文件系统中的物理路径。就网址而言,一套全新的术语将会出现。我希望有人能在同一条线索中填补空白:)我很高兴听到你所进行的公约。