f**********t 发帖数: 1001 | 1 (Coding) Write a basic file system and implement the commands ls, pwd, mkdir
, create, rm, cd, cat, mv. This was done on my own computer, in the
interview room, with a 1.5 hour time limit.
1.5小时,大牛提示下咋写?谢谢 | w**a 发帖数: 487 | 2 file system一般都是用tree,每个node就是一个folder,是不是这样啊?
mkdir
【在 f**********t 的大作中提到】 : (Coding) Write a basic file system and implement the commands ls, pwd, mkdir : , create, rm, cd, cat, mv. This was done on my own computer, in the : interview room, with a 1.5 hour time limit. : 1.5小时,大牛提示下咋写?谢谢
| j**********3 发帖数: 3211 | | f**********t 发帖数: 1001 | 4 自己先写点抛砖引玉好了,还没写完
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from collections import defaultdict
from os.path import join, getsize
# Write a basic file system and implement the commands ls, pwd, mkdir ,
# create, rm, cd, cat, mv.
class TreeNode:
def __init__(self, name, isDir):
self.name = name
self.children = defaultdict()
self.isDir = isDir
class FileSystem(object):
def __init__(self):
self.root = TreeNode("", True)
self.cur = self.root
def mkdir_(self, cur, nodes):
for node in nodes:
if node not in cur.children:
cur.children[node] = TreeNode(node, True)
cur = cur.children[node]
def mkdir(self, *args):
for arg in args:
nodes = arg.strip().split('/')
if nodes[0] == "": # absolute path
self.mkdir_(self.root, nodes[1:])
else:
self.mkdir_(self.cur, nodes)
# print (arg, ' ')
def dispatch(self, *args):
if len(args) < 1:
return
cmd = args[0].lower()
handler = getattr(self, cmd)
handler(*args[1:])
def ls_(self, cur, nodes):
for node in nodes:
if node not in cur.children:
return
cur = cur.children[node]
for v in self.cur.children.values():
print(v.name)
# print(v.name for v in self.cur.children.values())
def ls(self, *args):
if len(args) == 0:
ls_(self, self.cur, [])
else:
for arg in args:
nodes = arg.strip().split('/')
if nodes[0] == "":
self.ls_(self.root, nodes[1:])
else:
self.ls_(self.cur, nodes)
def walkDir(dir):
for root, dirs, files in os.walk(dir):
print(root, "consumes", end=" ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
def main():
fs = FileSystem()
while 1:
cmd = input(">> ")
# print(cmd)
fs.dispatch(*cmd.strip().split())
if __name__ == '__main__':
main()
【在 w**a 的大作中提到】 : file system一般都是用tree,每个node就是一个folder,是不是这样啊? : : mkdir
| w**********o 发帖数: 140 | 5 直接FUSE.
具體可以自己查一下FUSE API, 填空就好了. |
|