错误:无法将expanduser(‘~’)添加到dirs。’xxx’:’xxx"’

huangapple go评论79阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月7日 00:55:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定