2016-06-23 16 views
0

こんにちは1つのフォルダを移動した後に新しいフォルダを作成します。コードのスライス:私はこの古いパスを持っているバック

NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"\\NewInnerFolder") 
print NewFolder 

私は取得のみ:

\NewInnerFolder 

なぜですか?

答えて

2

たぶん、あなたは「// NewInnerFolder」を渡すと、 os.path.join は「絶対パス」として扱います

NewFolder = os.path.join(os.path.normpath(OldPath + os.sep + os.pardir),"NewInnerFolder") 
print NewFolder 

「//」なしで、以下のように書く必要があり、それをdrive_pathに参加させてください。

これは、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 
+0

はい、あなたは正しいです。ありがとう。これは私に1変数名を保存する –

関連する問題