我尝试在 bash 中实现一些代码,其输出类似于终端中的“tree”命令。
这里是:
listContent() {
local file
for file in "$1"/*; do
if [ -d $file ]; then
countSpaces $file
echo $?"Directory: $file"
listContent "$file"
elif [ -f $file ]; then
countSpaces $file
echo $?"File: $file"
fi
done
}
countSpaces() {
local space=" "
for (( i=0; i<${#$1}; i++ )); do
if [ ${file:$i:1} = "/" ]; then
space = space + space
return space
done
}
listContent "$1"
Run Code Online (Sandbox Code Playgroud)
运行我给出的脚本: ./scriptName.sh directoryName 其中 scriptName 是我的脚本,directoryName 是参数,它是代码应该启动的目录的名称。
我希望看到这样的输出:
Directory: Bash/Test1Dir …Run Code Online (Sandbox Code Playgroud) 我创建了名为 Student() 的类,其属性为 name 和 surname。在那个类中,我创建了类变量 -> all_students = []
创建对象时,它会被添加到该类变量 = all_students 中。
如何按属性名称的长度从最长到最短对对象列表进行排序?
import operator
class Student:
all_students = []
def __init__(self, name, surname):
self.name = name
self.surname = surname
Student.all_students.append(self)
s_1 = Student("Jacobbbb", "anything")
s_2 = Student("Mat", "anything1")
s_3 = Student("Marcooooooo", "sss")
new_list = sorted(Student.all_students, key=operator.attrgetter(len('name')))
for student in new_list:
print(student.name)
Run Code Online (Sandbox Code Playgroud)
我试过用运营商,但我不能这样做。将不胜感激的帮助。