2017-02-11 6 views
-2

ファイルは最終更新日に基づいて移動します。ボタン3のみを使用して、ディレクトリの最後にスラッシュがありません。ボタン付きPython askdirectory()

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__ 
    return self.func(*args) 
    File "C:/Python34/check2.py", line 30, in fileMove 
    if os.stat(src).st_mtime > now - 1 * 86400: 
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Somewhere/filenametextfile' 


import shutil, sys, time, os 
import tkinter as tk 
from tkinter import * 
import tkinter.filedialog 
root = Tk() 

def fileMove(): 
     sourcePath = filedialog.askdirectory() 
     receivePath = filedialog.askdirectory() 
     source = sourcePath 
     dest = receivePath 
     files = os.listdir(source) 

     now = time.time() 
     for f in files: 
      src = (source) +f 
      dst = (dest) +f 
      if os.stat(src).st_mtime > now - 1 * 86400: 
       if os.path.isfile(src): 
        shutil.move(src, dst) 
        print("File move is Alright, Alright, Alright") 

すべての表示を

def button_press(): 
     if one: 
      x = 1 
      display.configure(text=x) 
     if two: 
      x = 2 
      display.configure(text=x) 
     if three: 
      x = 3 
      display.configure(text=x) 

    display = LabelFrame(root, bg="red", width="462", height="60") 
    display.pack() 

    one = Button(root, text="1", width="15", height="5",command=fromSource) 
    one.pack(side="left") 

    two = Button(root, text="2", width="15", height="5", command=toReceive) 
    two.pack(side="left") 

    three = Button(root, text="3", width="15", height="5",command=fileMove) 
    three.pack(side="left") 

    root.mainloop() 
+0

あなたはスラッシュを追加するだけで試してみましたか?または 'os.path.join'を使用しますか? –

答えて

3

あなたが参加することos.path.joinメソッドを使用する必要があります。これは私が移動しようとしているテキストファイルをマージするパスを引き起こし単純な文字列連結ではなくパスを使用します。コードが複数のプラットフォームで動作します。この方法(Windowsの/ Mac用/ Linuxの)

は、例えば

from tkinter import Tk 
from tkinter import filedialog 
import os 

root = Tk() 
root.withdraw() 

current_directory = filedialog.askdirectory() 
file_name = "test.txt" 

file_path = os.path.join(current_directory,file_name) 
print(file_path) 
関連する問題