简体   繁体   English

如何将完整的文件路径拆分为一个路径和一个没有扩展名的文件名

[英]how to split a full file path into a path and a file name without an extension

how to split a full file path into a path and a file name without an extension?如何将完整的文件路径拆分为一个路径和一个没有扩展名的文件名? I'm looking for any files with the extension .conf: find /path -name .conf /path/file1.conf /path/smth/file2.conf /path/smth/file3.conf /path/smth/smth1/ .conf ... /path/smt/ /*.conf我正在寻找扩展名为.conf 的任何文件: find /path -name .conf /path/file1.conf /path/smth/file2.conf /path/smth/file3.conf /path/smth/smth1/ 。 conf ... /path/smt/ /*.conf

I need the output in string(without extension .conf): /path;file1|path/smth;file2;file3|...我需要字符串中的输出(不带扩展名 .conf):/path;file1|path/smth;file2;file3|...

What's the best way to do it?最好的方法是什么? I was thinking of a solution - save the output of the find work to a file and process them in a loop..but maybe there is a more effective way.我正在考虑一个解决方案 - 将查找工作的输出保存到一个文件中并在循环中处理它们......但也许有更有效的方法。 Sorry for mistakes, I newbie.. Thanx for u feedback, guys!对不起,我是新手……谢谢你的反馈,伙计们!

since you mentioned .conf , does this help?既然你提到了.conf ,这有帮助吗?

kent$ basename -s .conf '/path/smth/file2.conf'
file2

kent$ dirname '/path/smth/file2.conf'          
/path/smth

To do this in Bash:要在 Bash 中执行此操作:

find /path/ -type f -name "*.conf"

Note that if you want to do this in a Bash script, you can store /path/ in a variable, for instance one named directory, and change the command like so:请注意,如果您想在 Bash 脚本中执行此操作,您可以将 /path/ 存储在一个变量中,例如一个命名目录,并像这样更改命令:

find $directory -type f -name "*.conf"

To do this in Python:要在 Python 中执行此操作:

import os
PATH = /path/

test_files = [os.path.join(dp, f) for dp, dn, filenames in os.walk(PATH) for f in filenames
              if os.path.splitext(f)[1] == '.json']

There are some other ways to do this in Python listed here as well还有一些其他方法可以在此处列出的 Python 中执行此操作

bash parameter parsing is easy, fast, and lightweight. bash参数解析简单、快速且轻量级。

for fp in /path/file1.conf /path/smth/file2.conf /path/smth/file3.conf; do
  p="${fp%/*}"   # %  strips the pattern from the end       (minimal,   non-greedy)
  f="${fp##*/}"  # ## strips the pattern from the beginning (max-match, greedy)
  f="${f%.*}"    # end-strip the already path-cleaned filename to remove extention
  echo "$p, $f"
done
/path, file1
/path/smth, file2
/path/smth, file3

To get what you apparently want as your formatting -为了获得您显然想要的格式 -

declare -A paths                     # associative array
while read -r fp; do
  p=${fp%/*} f=${fp##*/};            # preparse path and filename
  paths[$p]="${paths[$p]};${f%.*}";  # p as key, stacked/delimited val 
done < file

Then stack/delimit your datasets.然后堆叠/分隔您的数据集。

for p in "${!paths[@]}"; do printf "%s|" "$p${paths[$p]}"; done; echo
/path;file1|/path/smth;file2;file3|

For each key, print key/val and a delimiter.对于每个键,打印 key/val 和一个分隔符。 echo at end for a newline. echo在末尾换行。

If you don't want the trailing pipe, assign it all to one var in the second loop instead of printing it out, and trim the trailing pipe at the end.如果您不想要尾管,请将其全部分配给第二个循环中的一个 var,而不是将其打印出来,并在最后修剪尾管。

$: for p in "${!paths[@]}"; do out="$out$p${paths[$p]}|"; done; echo "${out%|}"
/path;file1|/path/smth;file2;file3

Some folk will tell you not to use bash for anything this complex.有些人会告诉您不要将 bash 用于任何如此复杂的事情。 Be aware that it can lead to ugly maintenance, especially if the people maintaining it behind you aren't bash experts and can't be bothered to go RTFM.请注意,它可能会导致丑陋的维护,特别是如果在您身后维护它的人不是bash专家并且不会费心去 RTFM。

If you actually needed that embedded space in your example then your rules are inconsistent and you'll have to explain them.如果您在示例中确实需要该嵌入空间,那么您的规则不一致,您必须对其进行解释。

if you have the file paths in a list you can do this using a dictionary with key the path and value the filename如果您在列表中有文件路径,则可以使用带有键路径和值文件名的字典来执行此操作

aa=['/path/file1.conf','/path/smth/file2.conf','/path/smth/file3.conf']
f={}
for x in aa:
    temp=x[:-len(".conf")].split("/")
    filename=temp[-1]
    path="/".join(temp[:-1])
    if path in f:
        f[path]=f[path]+","+filename
    else:
        f[path]=filename
result=""
for x in f:
    result=result+str(x)+";"+f[x]+"|"
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM