2017-11-21 9 views
0
import os, shutil 

directory = 'C:\\Users\\MinJun\\Documents\\Python exercise solutions' 
def move_files(_dir): 
    for file in os.listdir(_dir): #check every file in directory 
     if os.path.isdir(file): #if it is a folder, skip 
      continue 
     if file.endswith('.py'): #if file ends with .py, skip 
      continue 
     else: #move file to newfolder, (it will automatically create one) 
      shutil.move(file, directory.join('\\newfolder')) 

move_files(directory) 

こんにちは、私は存在しないフォルダにフォルダやファイルの.pyでないファイルを移動しようとしている(であろうがいますshutil.moveで作成されます)。
FileNotFoundError:[Errno 2]そのようなファイルまたはディレクトリはありません: 'graphics'shutilとOSでの作業:[errnoを2]いいえそのようなファイルまたはディレクトリ「フォルダ」

myフォルダ 'graphics'がディレクトリ内の最初の項目です。 https://docs.python.org/3.5/library/stdtypes.html#str.join

をあなたはそれをもたらし、そしてこのパスは確かに存在しないためという何を見ることができますprint(directory.join('\\newfolder'))場合:

答えて

0

shutil.move(file, ''.join([directory, '\\newfolder']) 

をお試しくださいあなたが期待通りに動作しないだろうに参加します。

os.path.join(path, *paths)もあります。これは「パス認識」文字列結合関数の一種です。

関連する問題