Home > database >  My python program can run in D drive,but it cannot run in C and other drives
My python program can run in D drive,but it cannot run in C and other drives

Time:02-05

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("输入错误,请重新输入。")

When it runs in D drive ,it works well. But when it runs in other drives ,there is something wrong.

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'

Access the result of variables:

>>> f
<_io.TextIOWrapper name='C:\\Users\\14932\\AppData\\Roaming\\Tencent\\WeChat\\All Users\\config\\3ebffe94.ini' mode='r' encoding='cp936'>
>>> f.read()
''

I packed an exe file on another computer and put it on my computer to work properly. What is wrong with it? Could you help me? I do not know how to fix the bug. I asked other bbs,did not find the best ways.

CodePudding user response:

I believe the problem is here:

if f.read() == 'MyDocument:':
    location = 'C:'   users   '\Documents\WeChat Files'
else:
    location = f.read()   "\WeChat Files"

The first call to f.read() reads the entire contents of the file. There is nothing left to read on the second call, so it returns a blank string.

  •  Tags:  
  • Related