简体   繁体   English

将文件移动到子目录时,os.rename路径错误

[英]os.rename path error while moving file to subdirectory

I am new to python and coding in general. 我是python和编码的新手。 I have spent a lot of time trying to fix this error but I am not able to figure out how to do this. 我花了很多时间尝试修复此错误,但我不知道该怎么做。 I have a main folder that contains a subfolder, I want to move files from the main folder to subfolder. 我有一个包含子文件夹的主文件夹,我想将文件从主文件夹移动到子文件夹。 This should be done easily by os.rename or shutil.move but I am not able to fix this error. 应该可以通过os.renameshutil.move轻松完成此操作,但是我无法修复此错误。 Below is the code that I am using and the error that I am getting. 下面是我正在使用的代码以及我得到的错误。

cdir=os.getcwd()
newdir=cdir+"\subfolder"
src=os.path.join(cdir, fname) 
dst=os.path.join(newdir, fname)
os.rename(src,dst)

The error shows a double backslash in the directories path ie 错误显示目录路径中的双反斜杠,即

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'E:\\\\ folder\\\\fname' -> 'E:\\\\folder\\\\subfolder\\\\fname' FileNotFoundError:[WinError 3]系统找不到指定的路径:'E:\\\\ folder \\\\ fname'->'E:\\\\ folder \\\\ subfolder \\\\ fname'

the correct path would be with single back slashes. 正确的路径应该是单反斜线。 I am using windows 8.1 and python34. 我正在使用Windows 8.1和python34。 Can any one help me with this. 谁能帮我这个。 I know this question must be a duplicate but I am unable to understand what I am doing wrong. 我知道这个问题一定是重复的,但我无法理解自己在做什么错。 Similar error is generated with shutil.move shutil.move生成类似的错误

The double backslashes are normal ; 双反斜杠是正常的 ; they are not the cause of the error. 它们不是错误的原因。 Python always doubles up backslashes in string representations so that you can safely copy that value into a Python interpreter and reproduce the exact string: Python总是将字符串表示形式中的反斜杠加倍,以便您可以将该值安全地复制到Python解释器中并重新生成确切的字符串:

>>> print 'E:\\folder\\fname' 
E:\folder\fname
>>> 'E:\\folder\\fname' 
'E:\\folder\\fname'
>>> value = 'E:\\folder\\fname' 
>>> value
'E:\\folder\\fname'
>>> print value
E:\folder\fname

Python does this because a single backslash is used in escape sequences; Python之所以这样做,是因为在转义序列中使用了一个反斜杠。 '\\n' is a newline, but '\\\\n' is a backslash and the letter n . '\\n'是换行符,但'\\\\n'是反斜杠和字母n

Your error lies elsewhere; 您的错误出在其他地方; most likely the subfolder has yet to be created; 很有可能尚未创建subfolder os.rename() or shutil.move() will not create parent folders for you. os.rename()shutil.move()不会为您创建父文件夹。

You can use the os.makedirs() function to ensure that all folders in a path are created: 您可以使用os.makedirs()函数来确保创建路径中的所有文件夹:

newdir = os.path.abspath('subfolder')  # will use the current working directory
try:
    # ensure that it exists
    os.makedirs(newdir)
except OSError:
    pass  # it is already there
src = os.path.abspath(fname) 
dst = os.path.join(newdir, fname)
os.rename(src, dst)

You also need to make sure you don't accidentally use single backslashes in your filename or subfolder definitions; 您还需要确保不要在文件名或子文件夹定义中意外使用单个反斜杠。 \\s is not a valid escape, but others are valid and can produce unexpected results. \\s是不是一个有效的逃生,但其他人有效的,可以产生意想不到的结果。 Double the backslash in strings defining paths, or use a raw string literal , or use forward slashes instead: 将定义路径的字符串中的反斜杠加倍,或使用原始字符串文字 ,或改用正斜杠:

>>> '\new'  # newline!
'\new'
>>> print '\new'  # produces a blank line in between

ew
>>> '\\new'
'\\new'
>>> print '\\new'
\new
>>> r'\new'
'\\new'
>>> '/new'
'/new'

Windows accepts forward slashes just fine; Windows接受正斜杠就可以了; it doesn't care if the path separator is pointing forward or backward. 不管路径分隔符是指向前还是指向后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM