英文:
Error: The expanduser('~') cannot be added to dirs. 'xxx':'xxx"
问题
以下是您要翻译的部分:
我正在尝试从文件中读取目录并添加expanduser('~')
,但出现了错误。它没有添加C:\Users\kanna
。
dir_reader.py:
def dir_reader(x):
import os
f1 = open(x, 'r')
var0 = []
var2 = []
for data in f1:
var0.append(data)
home = os.path.expanduser('~')
for i in range(len(var0)):
var1 = var0[i]
var3 = var1.lstrip("'/'~").rstrip("'\n'")
vs = os.path.join(home, var3)
var2.append(vs)
return var2
output:
['C:\\OpenCity\\Premium\\premium_users.txt', 'C:\\OpenCity\\Premium\\premium_users.usra', 'C:\\OpenCity\\Plugins\\', 'C:\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.txt', 'C:\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.usra']
files.txt:
'~\\OpenCity\\Premium\\premium_users.txt'
'~\\OpenCity\\Premium\\premium_users.usra'
'~\\OpenCity\\Plugins\\'
'~\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.txt'
'~\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.usra'
英文:
I am trying to read a dirs from a file and add the expanduser('~')
But there is an error. It doesn't add 'C:\Users\kanna'
dir_reader.py:
def dir_reader(x):
import os
f1 = open(x, 'r')
var0 = []
var2 = []
for data in f1:
var0.append(data)
home = os.path.expanduser('~')
for i in range(len(var0)):
var1 = var0[i]
var3 = var1.lstrip("'/''~").rstrip("\'\n'")
vs = os.path.join(home, var3)
var2.append(vs)
return var2
output:
['C:\\OpenCity\\Premium\\premium_users.txt', 'C:\\OpenCity\\Premium\\premium_users.usra', 'C:\\OpenCity\\Plugins\\', 'C:\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.txt', 'C:\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.usra']
files.txt:
'~\OpenCity\Premium\premium_users.txt'
'~\OpenCity\Premium\premium_users.usra'
'~\OpenCity\Plugins\'
'~\OpenCity\Hacked_Version_Official\hacked_files_official_users.txt'
'~\OpenCity\Hacked_Version_Official\hacked_files_official_users.usra'
答案1
得分: 1
你正在去除错误的分隔符,这意味着 var3
将保持绝对路径,而 join
将丢弃除驱动器以外的 home
中的所有内容。
请改用 var3 = var1.strip("~/' \n")
。
英文:
You're stripping the wrong separator, which means var3
will remain an absolute path, and join
will throw away everything from home
other than the drive.
Use var3 = var1.strip("~\\/' \n")
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论