Python WindowsError: [错误 123] 文件名、目录名或卷标语法不正确:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33618656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect:
提问by AlliDeacon
I am new to programming, this is actually my first work assignment with coding. my code below is throwing an error:
我是编程新手,这实际上是我第一次编码工作。我下面的代码抛出错误:
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect.
I'm not able to find where the issue is.
我无法找到问题所在。
import os
folders = ["pdcom1", "pdcom1reg", "pdcomopen"]
for folder in folders:
path = r'"C:\Apps\CorVu\DATA\Reports\AlliD\Monthly Commission Reports\Output\pdcom1"'
for file in os.listdir(path):
print file
采纳答案by jkalden
As it solved the problem, I put it as an answer.
由于它解决了问题,我将其作为答案。
Don't use single and double quotes, especially when you define a raw string with r
in front of it.
不要使用单引号和双引号,尤其是当您r
在其前面定义原始字符串时。
The correct call is then
正确的调用是
path = r"C:\Apps\CorVu\DATA\Reports\AlliD\Monthly Commission Reports\Output\pdcom1"
or
或者
path = r'C:\Apps\CorVu\DATA\Reports\AlliD\Monthly Commission Reports\Output\pdcom1'
回答by Kevin Burns
I had a related issue working within Spyder, but the problem seems to be the relationship between the escape character ( "\") and the "\" in the path name Here's my illustration and solution (note single \ vs double \\ ):
我在 Spyder 中遇到了一个相关的问题,但问题似乎是转义字符(“\”)和路径名中的“\”之间的关系这是我的插图和解决方案(注意单 \ vs 双 \\ ):
path = 'C:\Users\myUserName\project\subfolder'
path # 'C:\Users\myUserName\project\subfolder'
os.listdir(path) # gives windows error
path = 'C:\Users\myUserName\project\subfolder'
os.listdir(path) # gives expected behavior
回答by user8996561
I had a similar issue while working with Jupyter. I was trying to copy files from one directory to another using copy function of shutil. The problem was that I had forgotten to import the package.(Silly) But instead of python giving import error, it gave this error.
我在使用 Jupyter 时遇到了类似的问题。我试图使用shutil的复制功能将文件从一个目录复制到另一个目录。问题是我忘记导入包了。(傻)但是python没有给出导入错误,而是给出了这个错误。
Solved by adding:
通过添加解决:
from shutil import copy
回答by Peter Smiley
I had this problem with Django and it was because I had forgotten to start the virtual environment on the backend.
我在 Django 上遇到了这个问题,这是因为我忘记在后端启动虚拟环境。
回答by Omkar
I was facing same error with Django Rest Framework, It was nothing to do with UI, still was getting this error. I applied below solution, worked for me.
我在使用 Django Rest Framework 时遇到了同样的错误,这与 UI 无关,仍然出现此错误。我应用了以下解决方案,对我有用。
- Restarted Machine.
- Restarted Virtual Environment.
- 重启机器。
- 重新启动虚拟环境。