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

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

Error: The expanduser('~') cannot be added to dirs. 'xxx':'xxx"

问题

以下是您要翻译的部分:

我正在尝试从文件中读取目录并添加expanduser('~'),但出现了错误。它没有添加C:\Users\kanna

dir_reader.py:

  1. def dir_reader(x):
  2. import os
  3. f1 = open(x, 'r')
  4. var0 = []
  5. var2 = []
  6. for data in f1:
  7. var0.append(data)
  8. home = os.path.expanduser('~')
  9. for i in range(len(var0)):
  10. var1 = var0[i]
  11. var3 = var1.lstrip("'/'~").rstrip("'\n'")
  12. vs = os.path.join(home, var3)
  13. var2.append(vs)
  14. return var2

output:

  1. ['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:

  1. '~\\OpenCity\\Premium\\premium_users.txt'
  2. '~\\OpenCity\\Premium\\premium_users.usra'
  3. '~\\OpenCity\\Plugins\\'
  4. '~\\OpenCity\\Hacked_Version_Official\\hacked_files_official_users.txt'
  5. '~\\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:

  1. def dir_reader(x):
  2. import os
  3. f1 = open(x, 'r')
  4. var0 = []
  5. var2 = []
  6. for data in f1:
  7. var0.append(data)
  8. home = os.path.expanduser('~')
  9. for i in range(len(var0)):
  10. var1 = var0[i]
  11. var3 = var1.lstrip("'/''~").rstrip("\'\n'")
  12. vs = os.path.join(home, var3)
  13. var2.append(vs)
  14. return var2

output:

  1. ['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:

  1. '~\OpenCity\Premium\premium_users.txt'
  2. '~\OpenCity\Premium\premium_users.usra'
  3. '~\OpenCity\Plugins\'
  4. '~\OpenCity\Hacked_Version_Official\hacked_files_official_users.txt'
  5. '~\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:

确定