double backslash python os.path.abspath

2019-09-03 00:30发布

I get the path

dire=os.path.abspath(".")

and

for fileName in filter(os.path.isfile, os.listdir(path=direc))

but dire has C:\\ and sends me the next error:

TypeError: listdir() takes no keyword arguments

when I print dire to see the content print next:

C:\\user\\documents....

what can I do to get \ and not \\ in os.path.abspath(".")?

1条回答
来,给爷笑一个
2楼-- · 2019-09-03 01:19

I'm assuming that by print you mean repr.

s = 'C:\\'
s
>>> 'C:\\'
print(s)
>> C:\

Note that while printing there aren't neither double \\ nor '

The other point is the error TypeError: listdir() takes no keyword arguments
so why dont try:

for fileName in filter(os.path.isfile, os.listdir(direc))

instead of

for fileName in filter(os.path.isfile, os.listdir(path=direc))
查看更多
登录 后发表回答