【问题标题】:Type error when changing directory更改目录时输入错误
【发布时间】:2013-12-08 11:50:33
【问题描述】:

我在尝试更改目录时不断收到以下错误..任何人都可以帮助解决问题以及如何解决这个问题?

代码片段:-

print "changing dir..."
print "BUILDDIR: " + BuildDir
os.chdir(BuildDir)
print "Done changing dir..."

错误:-

changing dir...
BUILDDIR: /local/mnt/workspace/username/AU_LINUX_ANDROID_CHO_3.5.04.03.00.003.047_msm8216/
Traceback (most recent call last):
  File "Main.py", line 682, in <module>
    flist = fd.readlines()
  File "Main.py", line 654, in main
    picked_gerrits = []
  File "Main.py", line 126, in RepoSync

TypeError: must be encoded string without NULL bytes, not str

【问题讨论】:

标签: python


【解决方案1】:

如果不能复制就很难说。试试:

'/'.join(map(str.strip, BuildDir.split('/')))

如果不是这样google brought me:

import os, re

_split = re.compile(r'[\0%s]' % re.escape(''.join(
    [os.path.sep, os.path.altsep or ''])))

def secure_filename(path):
    return _split.sub('', path)

print '/%s/' % '/'.join([secure_filename(x) for x in BuildDir.split('/') if x])

【讨论】:

  • 不清楚你的建议是什么......在做 os.chdir(BuildDir) 之前需要做什么 BuildDir 变量
  • 最后一试,请试一试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 2015-11-01
  • 1970-01-01
  • 2022-10-12
  • 2017-06-26
  • 1970-01-01
相关资源
最近更新 更多