path.dirname on Windows path is giving `.`

橙三吉。 提交于 2019-12-11 05:00:48

问题


I in electron am doing:

 path.dirname('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main')

That path is the actual value of my __dirname. How come it is not giving me C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron? I want that main part chopped off.


回答1:


Assuming main is a directory inside electron. Also assuming that you have some file called index.js inside main folder where you want to have the path of electron directory.

So, you can do path.join this way:

var mainFolderParentPath = path.join(__dirname, '../');

Your original file location:

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main\\index.js

__dirname will return

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main

and then inside path.join '../', will chop off the main folder from path. So, you will be left off with:

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron



回答2:


Well you obviously didn't read the docs for dirname. It states that it works like the Unix command dirname which "strips non-directory suffix from file name", thus you get the C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron.

What you are looking for is basename.

path.basename('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main') will give you main.



来源:https://stackoverflow.com/questions/45261574/path-dirname-on-windows-path-is-giving

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!