Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将1个文件夹移回后创建新文件夹_Python_Os.path - Fatal编程技术网

Python 将1个文件夹移回后创建新文件夹

Python 将1个文件夹移回后创建新文件夹,python,os.path,Python,Os.path,嗨,我有一条老路: OldPath = "C:\\Desktop\\Outerfolder\\InnerFolder" 并使用以下代码片段: os.path.normpath(OldPath + os.sep + os.pardir NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"\\NewInnerFolder") print NewFolder 我得到C:\Desktop\Outerfol

嗨,我有一条老路:

OldPath = "C:\\Desktop\\Outerfolder\\InnerFolder"
并使用以下代码片段:

os.path.normpath(OldPath + os.sep + os.pardir
NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"\\NewInnerFolder")
print NewFolder
我得到
C:\Desktop\Outerfolder
但在另一段代码中使用它:

os.path.normpath(OldPath + os.sep + os.pardir
NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"\\NewInnerFolder")
print NewFolder
我只得到:

\NewInnerFolder 

为什么会这样?

也许你应该像下面这样写,不带“/”

NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"NewInnerFolder")
print NewFolder
当您传递“//NewInnerFolder”时,os.path.join会将其视为“绝对路径”,并将其加入驱动器路径

这是python 3.5中os.path.join的源代码:

def join(path, *paths):
    if isinstance(path, bytes):
        sep = b'\\'
        seps = b'\\/'
        colon = b':'
    else:
        sep = '\\'
        seps = '\\/'
        colon = ':'
    try:
        if not paths:
            path[:0] + sep  #23780: Ensure compatible data type even if p is null.
        result_drive, result_path = splitdrive(path)
        for p in paths:
            p_drive, p_path = splitdrive(p)
            if p_path and p_path[0] in seps:
                # Second path is absolute
                if p_drive or not result_drive:
                    result_drive = p_drive
                result_path = p_path
                continue
            elif p_drive and p_drive != result_drive:
                if p_drive.lower() != result_drive.lower():
                    # Different drives => ignore the first path entirely
                    result_drive = p_drive
                    result_path = p_path
                    continue
                # Same drive in different case
                result_drive = p_drive
            # Second path is relative to the first
            if result_path and result_path[-1] not in seps:
                result_path = result_path + sep
            result_path = result_path + p_path
        ## add separator between UNC and non-absolute path
        if (result_path and result_path[0] not in seps and
            result_drive and result_drive[-1:] != colon):
            return result_drive + sep + result_path
        return result_drive + result_path
    except (TypeError, AttributeError, BytesWarning):
        genericpath._check_arg_types('join', path, *paths)
        raise

也许你应该像下面这样写,不带“/”

NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"NewInnerFolder")
print NewFolder
当您传递“//NewInnerFolder”时,os.path.join会将其视为“绝对路径”,并将其加入驱动器路径

这是python 3.5中os.path.join的源代码:

def join(path, *paths):
    if isinstance(path, bytes):
        sep = b'\\'
        seps = b'\\/'
        colon = b':'
    else:
        sep = '\\'
        seps = '\\/'
        colon = ':'
    try:
        if not paths:
            path[:0] + sep  #23780: Ensure compatible data type even if p is null.
        result_drive, result_path = splitdrive(path)
        for p in paths:
            p_drive, p_path = splitdrive(p)
            if p_path and p_path[0] in seps:
                # Second path is absolute
                if p_drive or not result_drive:
                    result_drive = p_drive
                result_path = p_path
                continue
            elif p_drive and p_drive != result_drive:
                if p_drive.lower() != result_drive.lower():
                    # Different drives => ignore the first path entirely
                    result_drive = p_drive
                    result_path = p_path
                    continue
                # Same drive in different case
                result_drive = p_drive
            # Second path is relative to the first
            if result_path and result_path[-1] not in seps:
                result_path = result_path + sep
            result_path = result_path + p_path
        ## add separator between UNC and non-absolute path
        if (result_path and result_path[0] not in seps and
            result_drive and result_drive[-1:] != colon):
            return result_drive + sep + result_path
        return result_drive + result_path
    except (TypeError, AttributeError, BytesWarning):
        genericpath._check_arg_types('join', path, *paths)
        raise

是的,你说得对。谢谢这给我留了一个变量名是的,你说得对。谢谢这将为我保存1个变量名