文件管理模拟系统,该系统包含的基本信息:创建用户、登录用户、创建文件、删除文件、打开文件、显示文件、关闭文件等信息,能显示文件内容、更改文件内容和更改文件名,并能修改文件的读写权限控制,支持查看文件的属性,主要包括:文件类型、文件长度、创建时间、文件权限源代码
时间: 2023-10-20 07:06:45 浏览: 63
以下是一个简单的文件管理模拟系统的源代码实现,主要包括创建用户、登录用户、创建文件、删除文件、打开文件、显示文件、关闭文件等功能,并支持文件内容的查看、更改、文件名的更改,以及文件权限的控制和查看文件属性。该代码采用Python语言实现。
```python
import os
import time
users = {} # 用户字典,用于存储已经注册的用户信息
current_user = None # 当前登录的用户
class User:
'''用户类,存储用户的基本信息'''
def __init__(self, name, password):
self.name = name
self.password = password
self.files = {} # 用户的文件字典,用于存储用户创建的文件信息
class File:
'''文件类,存储文件的基本信息'''
def __init__(self, name, content='', read_only=False):
self.name = name
self.content = content
self.read_only = read_only
self.create_time = time.time() # 文件的创建时间
self.length = len(content) # 文件长度
self.type = os.path.splitext(name)[1] # 文件类型
def create_user(name, password):
'''创建用户'''
if name in users:
print('用户名已经存在')
else:
users[name] = User(name, password)
print('用户创建成功')
def login(name, password):
'''登录用户'''
global current_user
if name not in users:
print('用户不存在')
return False
elif users[name].password != password:
print('密码错误')
return False
else:
current_user = users[name]
print('登录成功')
return True
def create_file(name, content=''):
'''创建文件'''
if current_user is None:
print('请先登录')
return
if name in current_user.files:
print('文件已经存在')
return
current_user.files[name] = File(name, content)
print('文件创建成功')
def delete_file(name):
'''删除文件'''
if current_user is None:
print('请先登录')
return
if name not in current_user.files:
print('文件不存在')
return
del current_user.files[name]
print('文件删除成功')
def open_file(name, mode='r'):
'''打开文件'''
if current_user is None:
print('请先登录')
return None
if name not in current_user.files:
print('文件不存在')
return None
file = current_user.files[name]
if mode == 'w' and file.read_only:
print('文件只读,无法写入')
return None
return file
def close_file(file):
'''关闭文件'''
if current_user is None:
print('请先登录')
return
if file is None:
return
file.length = len(file.content)
print('文件已关闭')
def read_file(file):
'''读取文件内容'''
if current_user is None:
print('请先登录')
return
if file is None:
return
print(file.content)
def write_file(file, content):
'''写入文件内容'''
if current_user is None:
print('请先登录')
return
if file is None:
return
if file.read_only:
print('文件只读,无法写入')
return
file.content = content
print('文件已保存')
def rename_file(old_name, new_name):
'''重命名文件'''
if current_user is None:
print('请先登录')
return
if old_name not in current_user.files:
print('文件不存在')
return
file = current_user.files[old_name]
if new_name in current_user.files:
print('文件名已存在')
return
file.name = new_name
current_user.files[new_name] = file
del current_user.files[old_name]
print('文件重命名成功')
def change_file_permission(name, read_only):
'''修改文件的读写权限'''
if current_user is None:
print('请先登录')
return
if name not in current_user.files:
print('文件不存在')
return
file = current_user.files[name]
file.read_only = read_only
print('文件权限修改成功')
def view_file_property(name):
'''查看文件属性'''
if current_user is None:
print('请先登录')
return
if name not in current_user.files:
print('文件不存在')
return
file = current_user.files[name]
print('文件名:', file.name)
print('文件类型:', file.type)
print('文件长度:', file.length)
print('创建时间:', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(file.create_time)))
print('读写权限:', '只读' if file.read_only else '读写')
# 测试代码
create_user('user1', '123456')
create_user('user2', '123456')
login('user1', '123456')
create_file('test.txt', 'hello world')
view_file_property('test.txt')
write_file(open_file('test.txt', 'w'), 'hello python')
read_file(open_file('test.txt'))
change_file_permission('test.txt', True)
rename_file('test.txt', 'test2.txt')
delete_file('test2.txt')
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)