i'm trying to code a patch programs with python to move translation files into the game.
i was able to make a program to select the file, but i'm suffering to move that selected file to the designated destination.
it says that python could not find the file "files", but i couldnt find the solution how to make it read the file.
import shutil
import os
from tkinter import filedialog
from tkinter import messagebox
list_file = []
files = filedialog.askopenfilenames(initialdir="/",\
title = "Select file")
if files == '':
messagebox.showwarning("Warning", "Add the file.")
destination = r'C:\Program Files (x86)\Steam\steamapps\common\I Wanna Maker\locale'
shutil.move("files", "destination")
Related
I want to make a list of files .max from a directory that the user can choose and then open it when the 3ds Max is not running.
The problem is that I am using os.starfile() to open the .max file and even tryied to use subprocess but none of them works and the following message appears:
"FileNotFoundError: [WinError 2] the system cannot find the file specified:"
As you can you see, the program recognizes the path of the directory and the files .max that are inside of it.
I've already used os.startfile to open a .max file once and it did it works, but can't figure it out why it's not working this time.
import os
import psutil
import easygui
import time
from tkinter import filedialog
from tkinter import *
root = Tk()
msg = "Select a folder to render the .max files inside"
title = "3DS MAX AUTO RENDER"
choice = ["Select Folder"]
reply = easygui.buttonbox(msg, title, choices = choice)
if reply == "Select Folder":
root.directoryPath = filedialog.askdirectory(initialdir="/", title="Select a folder")
print(root.directoryPath)
directoryFiles = os.listdir(root.directoryPath)
print(directoryFiles)
isRunning = "3dsmax.exe" in (i.name() for i in psutil.process_iter())
if (isRunning == False):
for file in directoryFiles:
os.startfile(file)
The file names returned by os.listdir are relative to the directory given. As you are not in that directory when you run the script it can't find the file which is why you get an error.
Use os.path.join to join the directory and file name:
directoryFiles = [os.path.join(root.directoryPath, x) for x in os.listdir(root.directoryPath)]
directoryFiles will then contain the absolute paths so you won't get the error.
Goal: Clean up duplicate data for specified file types in a drive. Amongst a series of duplicates, the file that remains should be the most recently modified.
Problem: It removes duplicates, but it does not discriminate by modified date/time.
I got this code from G4G. I've only added the "if file.endswith" part.
https://www.geeksforgeeks.org/deleting-duplicate-files-using-python/
from tkinter.filedialog import askdirectory
# Importing required libraries.
from tkinter import Tk
import os
import hashlib
from pathlib import Path
import time
# We don't want the GUI window of
# tkinter to be appearing on our screen
Tk().withdraw()
# Dialog box for selecting a folder.
file_path = askdirectory(title="Select a folder")
# Listing out all the files
# inside our root folder.
list_of_files = os.walk(file_path)
# In order to detect the duplicate
# files we are going to define an empty dictionary.
unique_files = dict()
for root, folders, files in list_of_files:
# Running a for loop on all the files
for file in files:
# Finding complete file path
file_path = Path(os.path.join(root, file))
# Converting all the content of
# our file into md5 hash.
Hash_file = hashlib.md5(open(file_path, 'rb').read()).hexdigest()
# If file hash has already #
# been added we'll simply delete that file
if Hash_file not in unique_files:
unique_files[Hash_file] = file_path
else:
if file.endswith((".txt",".bmp")):
os.remove(file_path)
print(f"{file_path} has been deleted")
Bonus Problem: In a drive that I am clearing of duplicates, there are 1148 folders. However, I do not want duplicates between the immediate different folders in the drive to be considered for removal. Is there a way to iterate this script over each folder instead of selecting each folder one by one?
I am currently trying to make a program for file organisation. I want my script to open a filedialog that asks for a sourcefolder where the files will be organised in based on their extension. After a sourcefolder has been selected, I want my script to make directories in that sourcefolder. I am a bit stuck on that last part.
import os
import shutil
from os import listdir
from os.path import isfile, join
import tkinter as tk
from tkinter import filedialog
from tkinter import *
print('This is a program that organises files in a given directory')
print(''
'')
print('Please select the folder in which you want to organise your files')
''' Source Folder '''
root = Tk()
root.withdraw()
source_path = filedialog.askdirectory()
The part above works just fine.
''' Create destination folders in the source folder '''
newpath1 = r'source_path\Images'
if not os.path.exists(newpath1): # check to see if they already exist
os.makedirs(newpath1)
newpath2 = r'source_path\Documents'
if not os.path.exists(newpath2):
os.makedirs(newpath2)
newpath3 = r'source_path\Else'
if not os.path.exists(newpath3):
os.makedirs(newpath3)
This, however, does not. It just removes files, or places them somewhere I cannot find them.
Any help would be greatly appreciated.
TLDR: how to make directories in a sourcefolder submitted with filedialog.
Can anyone tell me how I can get the for loop at the end of this program
NOT to rename folders, only files? I'm at a loss on this one.
I'm assuming the command to use is if os.path.isdir(file)
but I cant seem to get it to work no matter where I put it, same goes for os.path.isfile().
I'm still at the stage (my first week) where I am confused by most commands and functions, though I did dabble in ZX BASIC\AMOS and STOS in the 80s\90s so I have a rudimentary understanding of variables etc.
#FRenum-v.05
#renumbers a folder of files 01 onward preserving file extensions.
#steve Shambles. june 2018, my 2nd ever python program
from tkinter import filedialog
from tkinter import *
import os
import os.path
import subprocess
#user selects directory
root = Tk()
root.withdraw() #stop tk window opening
folder_selected = filedialog.askdirectory() #open file requestor
#change dir to folder selected by user,
os.chdir (folder_selected)
# read user selected dir
files = os.listdir(folder_selected)
# inc is counter to keep track of what file we are working on
inc = 1
for file in files:
#store file extension in string file_ext
file_ext = os.path.splitext(file)[1]
# build new filename, starting with a "0"
#then value of inc then add file ext
created_file=("0"+str(inc)+ file_ext)
#if filename does not already exist then rename it
if not os.path.exists(created_file):
os.rename(file,created_file)
#next one please, until done
inc = inc+1 #add to counter
#Display contents of folder in explorer
#https://stackoverflow.com/questions/50892257/beginner-opening-explorer-to- show-folder-contents
subprocess.Popen(["C:\\Windows\\explorer.exe", folder_selected.replace('/', '\\')])
#thanks to Michael for this line if code
load the modules can add it at the beginning of the script
from os.path import join , isfile
add this line before the loop:
files = [filenames for filenames in files if isfile(join(folder_selected, filenames))]
I'm starting to learn Python, and I thought to create a converter from a file to another (for example, from png to avi or between other file extensions) under Windows OS for now.
I wrote a script which works fine and it completes the conversion process, but I want improve it in functionality (and then in graphics); I'm using Tkinter and I thought to load the files with the possibility to drag-and-drop them as input for the next conversion command, instead of open a folder in which to put files as "input source". I found this topic (python drag and drop explorer files to tkinter entry widget) and I used it in this way:
import sys
import os
import Tkinter
from tkdnd_wrapper import TkDND
import shlex, subprocess
from subprocess import Popen, PIPE
import glob
import shutil
root = Tkinter.Tk()
dnd = TkDND(root)
entry = Tkinter.Entry()
entry.grid()
def handle(event):
inputfilespath = event.data
event.widget.insert(0, inputfilespath)
filesdir = os.path.dirname(os.path.realpath(inputfilespath))
files = glob.iglob(os.path.join(filesdir, "*.myext"))
for inputfilespath in files:
if os.path.isfile(inputfilespath):
subprocess1 = subprocess.Popen([...conversion command given as list, not string...], shell=True)
print "\n\nConversione in corso..."
subprocess1.wait()
subprocess1.terminate()
print "\n\nProcesso terminato!"
dnd.bindtarget(entry, handle, 'text/uri-list')
root.mainloop()
The problems:
If filename has a space, there is no conversion, and process ends without even notify any error too. "inputfilespath" wants to be the generic name for all the input files which I selected, and (for what I read) I can't (?) use quotes for an environment variable hoping to include filename's whitespace...
I tried to copy different files (with the same file extension and without whitespaces into the filename) in the same folder, and if I drag-and-drop only one of them on the Entry widget, the process starts fine (and this is great!), but it continues also for all the other no-selected files with the same extension in the same folder, whereas if I drag-and-drop multiple files on the Entry widget, no conversion occurs....
It seems that filenames containing whitespace are being wrapped in braces
(Tcl list style). To get a usable filelist, you should be able to do
something like:
import Tkinter
from untested_tkdnd_wrapper import TkDND
def handle(event):
files = root.tk.splitlist(event.data)
for filename in files:
event.widget.insert('end', filename)
root = Tkinter.Tk()
lb = Tkinter.Listbox(root, width=50)
lb.pack(fill='both', expand=1)
dnd = TkDND(root)
dnd.bindtarget(lb, handle, 'text/uri-list')
root.mainloop()
Just use the tkinter file dialog, then just have it insert the files into the entry box.
Example:
filedialog = tkFileDialog.askopenfilenames(*options*)
entry.insert(END, filedialog)
Example Using StringVar:
entryVar = StringVar()
entry = Entry(textvariable=entryVar)
filedialog = tkFileDialog.askopenfilenames(*options*)
entryVar.set(filedialog
Hope this helps!