首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的python程序可以在D驱动器中运行,但是它不能在C和其他驱动器中运行

我的python程序可以在D驱动器中运行,但是它不能在C和其他驱动器中运行
EN

Stack Overflow用户
提问于 2022-02-05 01:20:57
回答 1查看 145关注 0票数 0
代码语言:javascript
运行
复制
import os

def delete(files):
    os.system('del /f /s /q "%s\\*.*"' % files)
    print("清理成功!")
    
users = os.path.expandvars('$HOMEPATH')
f = open(r'C:' + users + '\\AppData\\Roaming\\Tencent\\WeChat\\All Users\\config\\3ebffe94.ini')
if f.read() == 'MyDocument:':
    location = 'C:' + users + '\Documents\WeChat Files'
else:
    location = f.read() + "\WeChat Files"
list = os.listdir(location)
list.remove('All Users')
list.remove('Applet')
print("""
""")
print(list)
print("""
""")
while True:
    temp = input("选择你要清理的微信号:")
    try:
        if 0<int(temp)<=len(list):
            temp1 = int(temp) - 1
            wxid = list[temp1]
            break
        else:
            print("输入错误,请重新输入。")
    except:
        print("输入错误,请重新输入。")
print("""
         -----------------------------Windows微信清理工具-------------------------------------

         ------------------------------【1.清理聊天记录】---------------------------------

         -----------------------------【2.清理图片和视频】-----------------------------------

         -----------------------------【3.清理接收到的文件】------------------------------

         ------------------------------【4.清理全部数据】-------------------------------

         """)
while True:
    choice = input("请输入要执行的操作所对应的代码:")
    if choice == '1':
        dialog = location + "\\" + wxid + '\Msg'
        delete(dialog)
        break
    elif choice == '2':
        pictures = location + "\\" + wxid + '\FileStorage\Image'
        delete(pictures)
        videos = location + "\\" + wxid + '\FileStorage\Video'
        delete(videos)
        break
    elif choice == '3':
        documents = location + "\\" + wxid + '\FileStorage\File'
        delete(documents)
        break
    elif choice == '4':
        delall = location + "\\" + wxid
        delete(delall)
        break
    else:
        print("输入错误,请重新输入。")

当它在D驱动器运行时,它工作得很好。但是当它在其他驱动器中运行时,会出现一些问题。

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "C:\Users\14932\Desktop\Windows微信清理工具.py", line 13, in <module>
    list = os.listdir(location)
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: '\\WeChat Files'

访问变量的结果:

代码语言:javascript
运行
复制
>>> f
<_io.TextIOWrapper name='C:\\Users\\14932\\AppData\\Roaming\\Tencent\\WeChat\\All Users\\config\\3ebffe94.ini' mode='r' encoding='cp936'>
>>> f.read()
''

我在另一台计算机上打包了一个exe文件,并将它放在我的计算机上才能正常工作。这是怎么回事?你能帮我一下吗?我不知道如何修复这个错误。我问了其他bbs,没有找到最好的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-05 01:32:27

我相信问题在于:

代码语言:javascript
运行
复制
if f.read() == 'MyDocument:':
    location = 'C:' + users + '\Documents\WeChat Files'
else:
    location = f.read() + "\WeChat Files"

f.read()的第一个调用读取文件的全部内容。在第二个调用中没有任何可读取的内容,因此它返回一个空字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70994576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档