路径组件的命名标准是什么?

发布于 2024-08-21 12:18:39 字数 990 浏览 6 评论 0原文

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

考虑以下玩具问题(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. 基本名称?基名?

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

C. 延期?

D. 延期?等等,这就是我所说的 C。
我是否应该避免存储点,而在需要时才将其放入?
如果特定文件上没有点怎么办?

E.?

F。?

G、文件夹?但这不是 Windows 特定的术语吗?

H. 路径名称?路径名?小路?

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

I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components.

Consider the following toy problem (Windows example, but hopefully the answer should be platform independent). You have been given the path of a folder:

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

You want to walk the folders underneath and compile all the .src files to .obj files.

At some point you are looking at the following path:

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

How would you name the following path components?

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

Here is my attempt:

A. Base name? Basename?

B. File name? Filename?
The difference is important when choosing identifier names, and I am never consistent here.

C. Extension?

D. Extension? Wait, that is what I called C.
Should I avoid storing the dot, and just put it in when required?
What if there is no dot on a particular file?

E. ?

F. ?

G. Folder? But isn’t this a Windows-specific term?

H. Path name? Pathname? Path?

I. File name? Wait, that is what I called B.
Path name? Wait, that is what I called H.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

半衬遮猫 2024-08-28 12:18:39

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


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

Vim 将其称为文件根(:help 文件名修饰符)


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

文件名或基本名称


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

文件/扩展名


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

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


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

树顶
没有约定,git 将其称为基目录


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

完整/绝对路径

I think your search for a "standard" naming convention will be in vain. Below are my proposals, based on existing, well-known programs.


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

Vim calls it file root (:help filename-modifiers)


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

file name or base name


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

file/name extension


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

also file extension. Simply store without the dot, if there is no dot on a file, it has no extension


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

top of the tree
No convention, git calls it base directory


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

path from top of the tree to the leaf
relative path


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

one node of the tree
no convention, maybe a simple directory


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

dir name


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

full/absolute path

烟燃烟灭 2024-08-28 12:18:39

首先,好问题,我的+1。当我不得不在 Utility 类中创建大量函数时,这件事让我很烦恼。获取文件名?或获取全名? GetApplicationPath 是指完整路径还是目录名?等等。我来自 .NET 背景,所以我想我可以为 @blinry 的优秀答案添加更多内容。

摘要:(斜体是我作为程序员不会使用的内容)

  1. 路径:路径指定文件系统中的唯一位置(除非它的相对路径)。路径名不太常用,但我会坚持使用路径 - 它几乎解释了它是什么。路径可以指向文件或文件夹,甚至不指向任何内容(C:\)。路径可以是:

    1. 相对路径My Source\Widget\ 是相对路径,Widget\foo.src 也是相对路径。不言自明。
    2. 绝对路径完整路径:是指向目标的完全限定路径。我倾向于更频繁地使用后者。 C:\users\OddThinking\Documents\My Source\Widget\foo.src 因此是完整路径。请参阅最后我所说的指向文件并以目录结尾的完整路径。

    wiki 页面路径的 .NET 命名是一​​致的.

  2. 根路径根目录:前者是 . NET 约定,而后者在 UNIX 圈中更为常见。虽然我两者都喜欢,但我更倾向于使用前者。与 UNIX 不同,Windows 中有许多不同的根路径,每个分区都有一个根路径。 Unix 系统有一个根目录,其中保存其他目录和文件的信息。例如。 C:\ 是根路径。

  3. 文件夹文件夹名称WidgetOddThinking等(根据您的情况)。这可能是 Windows 独有的约定(事实上这是我自己奇怪的想法:)),尽管如此,我强烈反对 blinry 的答案“目录”。虽然对于普通用户目录意味着与文件夹相同(如子文件夹、子目录),但我相信技术角度的“目录”听起来应该像是目标的合格地址,而不是目标本身。更多内容见下文。

    1. 子文件夹:对于用户而言,OddThinkingDocuments 是子文件夹。
    2. 子目录:针对用户OddThinking\OddThinking\Documents\OddThinking \Documents\My Source\Widget\ 是子目录。但我们并不经常需要为此操心,不是吗?
    3. 子文件夹:相对于用户 OddThinking 是子文件夹(以及子文件夹)
    4. 父文件夹:对于OddThinkingusers是其父文件夹(只是提到不同的术语,没什么大不了的)。
  4. 目录目录名称:前者在现实生活中普遍使用,后者在代码中使用。这是指直到目标的父文件夹为止的完全限定路径(或简称为完整路径)。在您的情况下,C:\users\OddThinking\Documents\My Source\Widget(是的,目录永远不会指向文件)。我在代码中使用目录名称,因为目录是 .NET 中的一个类,目录名称是库本身的名称。它与 UNIX 系统中使用的 dirname 非常一致。

  5. 文件名基本名称:文件的名称随着延伸。在您的情况下:foo.src。我想说,对于非技术用途,我更喜欢文件名(这对最终用户来说意味着什么),但出于技术目的,我会严格坚持基本名称。 MS 经常使用文件名,但令我惊讶的是它们不仅在文档中不一致,甚至在库中也不一致。文件名可以表示文件的基本名称或完整路径。所以我喜欢基本名称,这就是我在代码中对它们的称呼。 wiki 上的此页面也表示文件名可以表示完整路径或基本名称。令人惊讶的是,即使在 .NET 中,我也可以找到使用 basename 来表示文件的根名称。

  6. 扩展名文件扩展名文件扩展名:我喜欢最后一个。所有都指的是同一件事,但它是什么又是一个争论的问题! Wiki 说它是 src 而当时我记得读过许多语言将其解释为.src。注意点。因此,我的看法是,对于临时用途,它是什么并不重要,但作为一名程序员,我总是将扩展名视为 .src

    好吧,我可能尝试获取一些标准用法,但这是我遵循的两个约定。它与完整路径有关。

    1. 我通常将指向文件的完整路径称为文件路径。对我来说,文件路径很明确,它告诉我它是什么。虽然对于文件名,我发现它是文件的名称,但在我的代码中,我将其称为文件名。它也与“目录名称”一致。从技术角度来看,名称指的是完全限定名称!令人沮丧的是 .NET 使用术语文件名(所以我在这里有我的情况),有时也使用文件路径。

    2. 我将以目录结尾的完整路径称为目录。事实上,我们可以将任何不指向文件的地址称为目录。所以 C:\users\OddThinking\Documents\My Source\ 是一个目录,C:\users\OddThinking\ 是一个目录,甚至 OddThinking\Documents \My Source\ (最好将其称为子目录,甚至更好的相对路径 - 所有这一切都取决于您正在处理它的上下文)。上面我提到了一些关于目录的不同之处,即目录名称。这是我的看法:我将找到一条新的路径来避免混乱。 D:\Fruit\Apple\Pip\ 是什么?一个目录。但如果问题是D:\Fruit\Apple\Pip\的目录或者更好的目录名称是什么,答案是D:\Fruit\Apple\。希望它清楚。

    我想说最好不要担心最后两个术语,因为这会造成最大的混乱(对我个人而言)。只需使用术语完整路径

回答你:

  1. 关于你给出的路径

    A) 不知道。无论如何,我从来不需要单独得到那个。

    B) 基本名称

    C)我暂时将其称为文件扩展名,我最不担心,因为我从来不需要在代码中单独命名它。

    D) 文件扩展名肯定是。

    E) 我认为这不是通用目的要求。不知道。在.NET 中,基本目录与目录名称相同。

    F) 相对路径

    G) 文件夹(基本名称 foo.src 的父文件夹)

    H) 目录名称

    I)完整路径(甚至文件名)

  2. 一般情况下(抱歉,我是有点冗长,只是为了让大家明白这一点)但假设 foo.src 确实是一个文件

    A) 不适用

    B) 基本名称

    C) 不适用

    D) 扩展

    E) 目录或简单路径

    F) 相对路径

    G) 不适用

    H) 目录或简单路径

    I) 完整路径(甚至文件名)

用我这边的一个例子进一步说明:

  1. 考虑路径C:\Documents and Settings\All Users\Application Data\s.sql

    1. C:\Documents and Settings\All Users\Application Data\s.sql 是完整路径(即文件名)
    2. C:\Documents and Settings\All Users\Application Data\ 是目录名称。
  2. 现在考虑路径C:\Documents and Settings\All Users\Application Data

    1. C:\Documents and Settings\All Users\Application Data 是完整路径(恰好是一个目录)
    2. C:\Documents and Settings\All Users 是目录名称。

我的两个提示:

  1. 我遵循这条经验法则,即当涉及到寻址完整地址时,无论其类型如何,我几乎总是将其称为“完整路径” ”。这不仅消除了文件路径和文件夹路径这两个术语的使用,而且还避免了如果您要将文件名称命名为文件名(对于大多数用户而言,立即转换为基本名称)可能会产生混淆。但是,是的,如果您必须具体说明路径类型,最好命名文件名或目录,而不是更通用的“路径”。

  2. 无论是什么,你都会有自己的想法,并始终保持一致。团队成员之间达成共识,这意味着这个而不是那个。

现在,刚刚从圈子里出来的我已经有了一些练习。 OS X 和 Android 机器上使用的术语将是一个新品牌。所有这些都只是文件系统中的物理路径。就网址而言,将会出现一套全新的术语。我希望有人能填补同一条线索中的空白:)我很高兴听到您所进行的约定..

Good question first of all, my +1. This thing bugged me when I had to create a slew of functions in Utility class once. GetFileName? or GetFullName? GetApplicationPath means full path or the directory name? and so on. I come from .NET background, so I think I can add little more to otherwise excellent answer by @blinry.

Summary: (In italics is what I would not use as a programmer)

  1. 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:

    1. Relative Path: My Source\Widget\ is relative path as well as Widget\foo.src. Self explanatory.
    2. 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.

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

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

    1. Sub Folders: With respect to users OddThinking and Documents are sub folders.
    2. 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?
    3. Child Folder: With respect to users OddThinking is a child folder (as well as sub folder)
    4. Parent Folder: For OddThinking users is its parent folder (Just mentioning different terminologies, no big deal).
  4. 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.

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

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

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

    2. 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!

To answer you:

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

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

Further driving with one example from my side:

  1. Consider the path C:\Documents and Settings\All Users\Application Data\s.sql.

    1. C:\Documents and Settings\All Users\Application Data\s.sql is the full path (which is a file name)
    2. C:\Documents and Settings\All Users\Application Data\ is the directory name.
  2. Now consider the path C:\Documents and Settings\All Users\Application Data

    1. C:\Documents and Settings\All Users\Application Data is the full path (which happens to be a directory)
    2. C:\Documents and Settings\All Users is the directory name.

Two tips of mine:

  1. 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".

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

Now that just from the circle I have some practice. A new brand of terms would be what is used on OS X and android machines. And all these are just about physical paths in filesystem. A whole new set of terminologies would arise in case of web addresses. I expect someone to fill the void in this same thread :) I would be glad to hear the convention with which you have went ahead..

ぶ宁プ宁ぶ 2024-08-28 12:18:39

在 C++ 中,Boost.Filesystem 设计了一个路径各个部分的命名法。请参阅路径分解参考有关详细信息的文档,以及此 教程

这是基于教程的摘要。对于:

  • Windows 路径:c:\foo\bar\baa.txt
  • Unix 路径:/foo/bar/baa.txt

您将得到:

Part            Windows          Posix
--------------  ---------------  ---------------
Root name       c:               <empty>
Root directory  \                /
Root path       c:\              /
Relative path   foo\bar\baa.txt  foo/bar/baa.txt
Parent path     c:\foo\bar       /foo/bar
Filename        baa.txt          baa.txt
Stem            baa              baa
Extension       .txt             .txt

C++ 标准 ISO/IEC 14882: 2017

此外,Boost.Filesystem 术语已被 C++17 => 采用请参阅 std::filesystem

Function name     Meaning
----------------  -------------------------------
root_name()       Root-name of the path
root_directory()  Root directory of the path
root_path()       Root path of the path
relative_path()   Path relative to the root path
parent_path()     Path of the parent path
filename()        Path without base directory (basename)
stem()            Filename without extension
extension()       Component after last dot

In C++, Boost.Filesystem has devised a nomenclature for the various parts of a path. See the path decomposition reference documentation for details, as well as this tutorial.

Here's a summary based on the tutorial. For:

  • Windows path: c:\foo\bar\baa.txt
  • Unix path: /foo/bar/baa.txt

you get:

Part            Windows          Posix
--------------  ---------------  ---------------
Root name       c:               <empty>
Root directory  \                /
Root path       c:\              /
Relative path   foo\bar\baa.txt  foo/bar/baa.txt
Parent path     c:\foo\bar       /foo/bar
Filename        baa.txt          baa.txt
Stem            baa              baa
Extension       .txt             .txt

C++ standard ISO/IEC 14882:2017

Moreover Boost.Filesystem terminology has been adopted by C++17 => See std::filesystem

Function name     Meaning
----------------  -------------------------------
root_name()       Root-name of the path
root_directory()  Root directory of the path
root_path()       Root path of the path
relative_path()   Path relative to the root path
parent_path()     Path of the parent path
filename()        Path without base directory (basename)
stem()            Filename without extension
extension()       Component after last dot
梦里梦着梦中梦 2024-08-28 12:18:39

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

路径描述
A./x/y/z.tar.gz词干
B./x/y/z.tar.gz名称
C./x/y/z .tar.gz(不包括点)N/A
D./x/y/z.tar.gz(包括点)后缀
E./x< /code>/y/z.tar.gz祖父路径
F./x/y/z.tar.gz祖父路径
G./x/y 的相对路径code>/z.tar.gz父名称
H./x/y/z.tar.gz父路径
I./x/y/z.tar.gz小路

The Pathlib standard library in Python follows this naming convention for path components:

PathDescription
A./x/y/z.tar.gzstem
B./x/y/z.tar.gzname
C./x/y/z.tar.gz (excluding dot)N/A
D./x/y/z.tar.gz (including dot)suffix
E./x/y/z.tar.gzgrand parent path
F./x/y/z.tar.gzrelative path to grand parent path
G./x/y/z.tar.gzparent name
H./x/y/z.tar.gzparent path
I./x/y/z.tar.gzpath
灼疼热情 2024-08-28 12:18:39

不,你没有疯。

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

    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 方法更加合乎逻辑,这就是上面每个人都提到的:包含文件名本身的路径。但是,如果您输入“call /?”在 Windows 命令行中,您会得到以下结果:

    %~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

ADEF 没有简单的昵称。由于 php 可能是最广为人知的跨平台语言,每个人都理解“basename”和“dirname”,所以我会坚持使用这个命名。全名也很明显;完整路径可能有点含糊,但大多数时候它意味着同样的事情。

No you're not crazy.

In Windows systems, sometimes the path of the directory containing the file is called path, which is how it was from the beginning. So, for example,

    x:\dir1\dir2\myfile.txt

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

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

The Unix/Linux approach is a lot more logical, and that's what everyone mentioned above: path including the file name itself. However, if you type "call /?" in the Windows command line, you get this:

    %~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

So there it is, "path only" and "file name only". At the same time, they refer to the whole string as "fully qualified path name" which is understood as drive letter plus path plus file name. So there's no real truth. It's futile. You've been betrayed.

Anyway,

To answer your question

This is how I'd name your examples:

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

A-D-E-F have no simple nicknames. And since php is probably the most widely known cross-platform language, everyone understands "basename" and "dirname" so I'd stick with that naming. Full name is also obvious; full path would be a bit ambiguous but most of the time it means the very same thing.

江南烟雨〆相思醉 2024-08-28 12:18:39

经过 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

After 10 years of hacking around my two pence is:

  • Be consistent
  • Think recursion

enter image description here

Example on a Windows machine:

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: .\

The linux stuff near the bottom is how bash mounts the drive on Windows systems.

The current directory, or working "directory", is really wherever your program is, but let's use it to keep track of where the current file is we are working on. Type pwd into powershell and the result is called a path!

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.

That is, paths can be formed by adding either the root, filename, or both, to a directory. (you could differentiate between paths and file paths, the 'relative path' would then exclude the file name but give the directories from the base to the working directory, though the term becomes redundant as this is properly called the relative directory).

Notice the distinct meanings of keywords:

  • name
  • directory
  • path
  • separator

These are then combined with the parts of the full path:

  • root
  • base
  • relative
  • file

Example: root path = root name + root directory

Notice how this works for both Windows and Linux (where the root path is the same as the root directory because the root name is empty).


In Java, the output is produced by:

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);
    }
}

To give an answer to the OP's question:

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
南风几经秋 2024-08-28 12:18:39
  • foo 不带扩展名的文件名

  • foo.src 文件名

  • src 扩展名

  • .src 也许带有点的扩展,但是不应该使用这个。正如所写的,这可以是目录名或文件名。

  • C:\users\OddThinking\Documents\My Source\ [绝对]目录路径

  • Widget\foo.src 相对文件路径

  • Widget 目录名称

  • C:\users\OddThinking\Documents\My Source\Widget\ 这仍然是一个绝对目录路径。如果一个是 root 而另一个不是,则由您的变量名称来跟踪它,那里没有真正的语义差异。


  • C:\users\OddThinking\Documents\My Source\Widget\foo.src [绝对] 文件路径

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

单词“目录”可以替换为单词“文件夹”。 (也许我们应该在较低级别使用“目录”,但我更喜欢“文件夹”更短。)

实际上可以创建所有这些类型的语义框架,并使用语义上有效的函数来组合它们。例如,FolderNameFilename 可以组合起来给出 RelativeFilePathFolderPath(隐含绝对值)和 RelativeFilePath 可以组合起来给出 FilePath(隐含绝对值)。

此外,其中一些是相关的;例如,FilenameWithoutExtension 是一种Filename,因此应该是可转换的。 FolderNameRelativeFolderPath,因此应该是可转换的。 ETC。

  • 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" is a word, so generally we should use "filename" and not "file name" (and Filename not FileName).

The word "Directory" can be replaced with the word "Folder". (Maybe we should use "Directory" at a lower level, but "Folder" is shorter which I prefer.)

It's actually possible to create a semantic framework of all of these types, with semantically valid functions for combining them. For example, a FolderName and a Filename can be combined to give a RelativeFilePath. A FolderPath (absolute is implied) and a RelativeFilePath can be combined to give a FilePath (absolute is implied).

Also, some of these are related; for example a FilenameWithoutExtension is a kind of Filename, so should be convertible. A FolderName is a RelativeFolderPath, so should be convertible. Etc.

套路撩心 2024-08-28 12:18:39

对于简单的项目,您可以采用简单的答案:
没有使用 path 这个词,因为如果也使用 URL/路径,则可以将其与 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              |

Simple answer you could adopt for simple projects:
Have not used the word path because its useful to differentiate it with web paths if working with URLs/path too.

| 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              |
单身情人 2024-08-28 12:18:39

路径组件的命名标准是什么?

无论是什么,您最终都会使用最适合您的。这是我的方法。


A: “foo”

  • 文件名fn

B: “foo.src”

  • 文件f

C: “src”

  • extension_nameext_namexn

D: “.src”

  • 扩展名extfx

E: “C:\Users\OddThinking\Documents\My Source”

  • basedirectorybasedir BD

F: “Widget\foo.src”

  • [relative_]文件路径,[rel_]文件路径,[rel_] fp

G:“小部件”

  • [relative_]子目录、[rel_]子目录、[rel_]sd

H: “C:\Users\OddThinking\Documents\My Source\Widget”

  • [absolute_]子目录,[abs_]子目录,[abs_]sd

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

  • [absolute_]文件路径,[abs_] 文件路径,[abs_]fp

示例:

f = fn + fx
file = filename + ext
file = filename + extension

f = fn + "." + xn
file = filename + "." + ext_name
file = filename + "." + extension_name

[abs_]fp = bd + [rel_]fp
[abs_]filepath = basedir + [rel_]filepath
[absolute_]filepath = basedirectory + [relative_]filepath

[abs_]fp = bd + [rel_]sd + f
[abs_]filepath = basedir + [rel_]subdir + file
[absolute_]filepath = basedirectory + [relative_]subdirectory + file

[abs_]fp = [abs_]sd + f
[abs_]filepath = [abs_]subdir + file
[absolute_]filepath = [absolute_]subdirectory + file

What is the naming standard for path components?

Whatever it is, you will end up using what suits you best. Here's my approach.


A: "foo"

  • filename, fn

B: "foo.src"

  • file, f

C: "src"

  • extension_name, ext_name, xn

D: ".src"

  • extension, ext, fx

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

  • basedirectory, basedir, bd

F: "Widget\foo.src"

  • [relative_]filepath, [rel_]filepath, [rel_]fp

G: "Widget"

  • [relative_]subdirectory, [rel_]subdir, [rel_]sd

H: "C:\Users\OddThinking\Documents\My Source\Widget"

  • [absolute_]subdirectory, [abs_]subdir, [abs_]sd

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

  • [absolute_]filepath, [abs_]filepath, [abs_]fp

Examples:

f = fn + fx
file = filename + ext
file = filename + extension

f = fn + "." + xn
file = filename + "." + ext_name
file = filename + "." + extension_name

[abs_]fp = bd + [rel_]fp
[abs_]filepath = basedir + [rel_]filepath
[absolute_]filepath = basedirectory + [relative_]filepath

[abs_]fp = bd + [rel_]sd + f
[abs_]filepath = basedir + [rel_]subdir + file
[absolute_]filepath = basedirectory + [relative_]subdirectory + file

[abs_]fp = [abs_]sd + f
[abs_]filepath = [abs_]subdir + file
[absolute_]filepath = [absolute_]subdirectory + file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文