清理C盘空间的代码可以使用以下方法:
1. 删除临时文件:
```python
import os
def delete_temp_files():
temp_folder = "C:\\Users\\YourUser\\AppData\\Local\\Temp"
for root, dirs, files in os.walk(temp_folder):
for file in files:
file_path = os.path.join(root, file)
os.remove(file_path)
```
这段代码将删除Windows系统临时文件夹中的所有文件。替换YourUser为你的用户名。
2. 清理回收站:
```python
import send2trash
def empty_recycle_bin():
send2trash.send2trash('C:\\$RECYCLE.BIN')
```
用此代码清空回收站。
3. 删除不再需要的程序:
```python
import winreg
def uninstall_program(program_name):
uninstall_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, uninstall_key) as key:
for i in range(winreg.QueryInfoKey(key)[0]):
sub_key_name = winreg.EnumKey(key, i)
with winreg.OpenKey(key, sub_key_name) as sub_key:
try:
display_name = winreg.QueryValueEx(sub_key, 'DisplayName')[0]
if program_name in display_name:
uninstall_string = winreg.QueryValueEx(sub_key, 'UninstallString')[0]
os.system(uninstall_string)
except FileNotFoundError:
pass
```
使用此函数卸载指定的程序。
4. 清理系统文件:
```python
import subprocess
def clean_system_files():
subprocess.call(['cleanmgr', '/sagerun:1', '/veryclean'])
```
这将运行Windows的磁盘清理工具,并删除系统文件。
5. 删除临时Internet文件:
```python
import tempfile
import shutil
def delete_internet_temp_files():
internet_temp_folder = os.path.join(tempfile.gettempdir(), 'Temporary Internet Files')
if os.path.exists(internet_temp_folder):
shutil.rmtree(internet_temp_folder)
```
使用此函数删除临时Internet文件。
6. 压缩文件夹:
```python
import shutil
def compress_folder(folder_path):
shutil.make_archive(folder_path, 'zip', folder_path)
shutil.rmtree(folder_path)
```
这将压缩指定文件夹为zip文件,并删除原始文件夹。
以上是一些常见的清理C盘空间的代码示例,你可以根据需要选择使用。请注意,在运行这些代码之前,请仔细检查并备份重要的文件,以免意外删除了错误的文件。
声明:免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:dm@cn86.cn进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。本站原创内容未经允许不得转载。