Pythonを使用してファイルシステムシミュレーションクラスを実装する
説明内容: シンプルなファイルシステムシミュレーションクラスをPythonで実装します。このクラスでは、ディレクトリの作成、ファイルの作成、ファイルの削除、ディレクトリ内容の一覧表示などの基本操作をユーザーに提供します。ファイルシステムの階層構造をシミュレートするために辞書を使用します。
コード部分:
サンプル
class FileSystem:
def __init__(self):
self.root = {'/': {}}
def mkdir(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
for part in parts:
if part not in current:
current[part] = {}
current = current[part]
def touch(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
filename = parts[-1]
dir_path = parts[:-1]
for part in dir_path:
if part not in current:
current[part] = {}
current = current[part]
current[filename] = None
def ls(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
for part in parts:
if part not in current:
return f"Path '{path}' not found."
current = current[part]
if isinstance(current, dict):
return list(current.keys())
else:
return [parts[-1]]
def rm(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
filename = parts[-1]
dir_path = parts[:-1]
for part in dir_path:
if part not in current:
return f"Path '{path}' not found."
current = current[part]
if filename in current:
del current[filename]
return f"File '{filename}' deleted."
else:
return f"File '{filename}' not found."
# Example usage
fs = FileSystem()
fs.mkdir('/home/user')
fs.touch('/home/user/file.txt')
print(fs.ls('/home/user')) # Output: ['file.txt']
fs.rm('/home/user/file.txt')
print(fs.ls('/home/user')) # Output: []
def __init__(self):
self.root = {'/': {}}
def mkdir(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
for part in parts:
if part not in current:
current[part] = {}
current = current[part]
def touch(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
filename = parts[-1]
dir_path = parts[:-1]
for part in dir_path:
if part not in current:
current[part] = {}
current = current[part]
current[filename] = None
def ls(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
for part in parts:
if part not in current:
return f"Path '{path}' not found."
current = current[part]
if isinstance(current, dict):
return list(current.keys())
else:
return [parts[-1]]
def rm(self, path):
current = self.root['/']
parts = path.strip('/').split('/')
filename = parts[-1]
dir_path = parts[:-1]
for part in dir_path:
if part not in current:
return f"Path '{path}' not found."
current = current[part]
if filename in current:
del current[filename]
return f"File '{filename}' deleted."
else:
return f"File '{filename}' not found."
# Example usage
fs = FileSystem()
fs.mkdir('/home/user')
fs.touch('/home/user/file.txt')
print(fs.ls('/home/user')) # Output: ['file.txt']
fs.rm('/home/user/file.txt')
print(fs.ls('/home/user')) # Output: []
コード解析:
__init__メソッドはファイルシステムを初期化し、ルートディレクトリを作成します/。mkdirメソッドはディレクトリを作成するために使用します。パスの各部分を走査して、ネストされた辞書構造を作成します。touchメソッドはファイルを作成するために使用します。パスの最後の部分をファイル名として、対応するディレクトリにキーと値のペアを作成します。lsメソッドはディレクトリの内容を一覧表示するために使用します。パスを走査し、ディレクトリ内のキー(ファイルまたはサブディレクトリの名前)を返します。rmメソッドはファイルを削除するために使用します。パスを走査し、指定されたファイルを削除します。
出力結果:
サンプル
['file.txt']
[]
[]
Python3 サンプル