I am looking for an simple file chooser dialog in sl4a. I have found a few native dialogs here but did not find the one I was looking for.
I wish I could save time by finding something readily available. A minimal code like filename = fileopendialog() would be a bonus.
Any ideas ?
I decided to write my own (see below for reference). This could probably be made better, any suggestions welcome.
import android, os, time
droid = android.Android()
# Specify root directory and make sure it exists.
base_dir = '/sdcard/sl4a/scripts/'
if not os.path.exists(base_dir): os.makedirs(base_dir)
def show_dir(path=base_dir):
"""Shows the contents of a directory in a list view."""
#The files and directories under "path"
nodes = os.listdir(path)
#Make a way to go up a level.
if path != base_dir:
nodes.insert(0, '..')
droid.dialogCreateAlert(os.path.basename(path).title())
droid.dialogSetItems(nodes)
droid.dialogShow()
#Get the selected file or directory .
result = droid.dialogGetResponse().result
droid.dialogDismiss()
if 'item' not in result:
return
target = nodes[result['item']]
target_path = os.path.join(path, target)
if target == '..': target_path = os.path.dirname(path)
#If a directory, show its contents .
if os.path.isdir(target_path):
show_dir(target_path)
#If an file display it.
else:
droid.dialogCreateAlert('Selected File','{}'.format(target_path))
droid.dialogSetPositiveButtonText('Ok')
droid.dialogShow()
droid.dialogGetResponse()
if __name__ == '__main__':
show_dir()
Related
I am doing a project related to computer vision. As I always like to write neat code (even though I have no formal training in coding), I have to resort to asking some questions here. Please bear with me.
There are a few things that I want to achieve with the following function.
One is It checks the extension type, and make sure they are of jpg, jpeg, or png (but this list is customizable).
Two is It checks the image folder and make sure they are of all the same extension type, no surprises like 100 images with '.jpg' while the rest are '.png'.
Lastly, The function should finally return the extension type, so that I can do something like this in my pipeline:
image_path = os.path.join(path_to_image, image_id+check_file_type(image_folder_path))
PS: Is there any way to join two strings without using + operator, it
really looks ugly (but it is just my pet peeve).
My function is as follows, I always feel there is something wrong with it, although it seems to work fine, I would appreciate someone with experience to correct/improve my function and not look cumbersome.
from collections import Counter
from tqdm import tqdm
from type import Optional, List
def check_file_type(image_folder_path, allowed_extensions:Optional[List]=None):
if allowed_extensions is None:
allowed_extensions = ['.jpg', '.png', '.jpeg']
extension_type = []
file_list = os.listdir(image_folder_path)
for file in tqdm(file_list):
extension_type.append(os.path.splitext(file)[-1].lower())
extension_dict = Counter(extension_type)
assert len(extension_dict.keys()) == 1, "The extension in the folder should all be the same, but found {} extensions".format(extension_dict.keys)
extension_type = list(extension_dict.keys())[0]
assert extension_type in allowed_extensions
return extension_type
If you don't want to use collections and don't want to iterate over each file in the folder, then you can do something like this.
import glob
def check_file_type(image_folder_path, allowed_extensions=None):
if allowed_extensions is None:
allowed_extensions = ['.jpg', '.png', '.jpeg']
no_files_in_folder = len(glob.glob(image_folder_path+"/*"))
extension_type = ""
no_files_allowed = 0
for ext in allowed_extensions:
no_files_allowed = len(glob.glob(image_folder_path+"/*"+ext))
if no_files_allowed > 0:
extension_type = ext
break
assert no_files_in_folder == no_files_allowed, "The extension in the folder should all be the same, but found more than one extensions"
return extension_type
First get the total number of files you have in the folder
Then get the number of files for each allowed extension, if glob gets more than one file for an extension then loop will break and assert will check that whether no_files_in_folder == no_files_allowed
if true it will return the extension type
if false then surely there are files with more than one extension in that folder
import glob
from pathlib import Path
import os
folder_path = Path.home() / "Desktop"
# Set of allowed extensions
allowed_exts = {".png", ".jpg"}
# Set of extensions present in the directory
extensions_present = {
Path(i).suffix
for i in glob.glob(str(folder_path / "*"))
if Path(i).suffix # do not take empty '' strings.
}
print(extensions_present)
print(extensions_present - allowed_exts)
if len(extensions_present - allowed_exts) > 0:
print("Unrelated extensions found.")
You can construct a set of allowed extensions and then perform set difference to figure out if there are unrelated extensions.
I am doing a school assignment where I have to take input from a user and save it to a text file.
My file structure will be something like:
- Customer register
- Customer ID
- .txt files 1-5
It can be saved in the python folder and I can make the folders like this:
os.makedirs("Customer register/Customer ID")
My question is, how do I set the path the text files are to be stored in, in the directory when I don't know the directory? So that no matter where the program is run it is saved in the "Customer ID" folder I create (but on the computer the program is run on)?
Also, how do I make this work on both windows and mac?
I also want to program to be able to be executed several times, and check if the folder is there and save to the "Customer ID" folder if it already exists. Is there a way to do that?
EDIT:
This is the code I am trying to use:
try:
dirs = os.makedirs("Folder")
path = os.getcwd()
os.chdir(path + "/Folder")
print (os.getcwd())
except:
if os.path.exists:
path = os.getcwd()
unique_filename = str(uuid.uuid4())
customerpath = os.getcwd()
os.chdir(customerpath + "/Folder/" + unique_filename)
I am able to create a folder and change the directory (everything in "try" works as I want).
When this folder is created I want to create a second folder with a random generated folder name (used for saving customer files). I can't get this to work in the same way.
Error:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\Users\48736\PycharmProjects\tina/Folder/979b9026-b2f6-4526-a17a-3b53384f60c4'
EDIT 2:
try:
os.makedirs("Folder")
path = os.getcwd()
os.chdir(path + "/Folder")
print (os.getcwd())
except:
if os.path.exists:
path = os.getcwd()
os.chdir(os.path.join(path, 'Folder'))
print(os.getcwd())
def userId(folderid):
try:
if not os.path.exists(folderid):
os.makedirs(folderid)
except:
if os.path.exists(folderid):
os.chdir(path + "/Folder/" + folderid)
userId(str(uuid.uuid4()))
print(os.getcwd())
So I can now create a folder, change directory to the folder I have created and create a new folder with a unique filename within that folder.
But I can't change the directory again to the folder with the unique filename.
Any suggestions?
I have tried:
os.chdir(path + "/Folder/" + folderid)
os.chdir(path, 'Folder', folderid)
os.chdir(os.path.join(path, 'Folder', folderid))
But is still just stays in: C:\Users\47896\PycharmProjects\tina\Folder
You can use relative paths in your create directory command, i.e.
os.makedirs("./Customer register/Customer ID")
to create folder in project root (=where the primary caller is located) or
os.makedirs("../Customer register/Customer ID") in parent directory.
You can, of course, traverse the files tree as you need.
For specific options mentioned in your question, please, see makedirs documentation at Python 3 docs
here is solution
import os
import shutil
import uuid
path_on_system = os.getcwd() # directory where you want to save data
path = r'Folder' # your working directory
dir_path = os.path.join(path_on_system, path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
file_name = str(uuid.uuid4()) # file which you have created
if os.path.exists(file_name) and os.path.exists(dir_path):
shutil.move(file_name,os.path.join(dir_path,file_name))
else:
print(" {} does not exist".format(file_name))
I use GIMP for some batch editing, part of this is exporting painting with the filename taken from original image "filename" with this code:
pdb.file_png_save_defaults(ima, drawable, fullpath, filename)
At this moment, I have
fullpath=filename
so it saves the image into same folder as the source and filename and fullpath are identical.
What I want to do is to put it into subdirectory in this folder, lets call it "subfolder". But if I try:
fullpath = '\subfolder\'+filename
I get an error, obviously, because I am working with Python (or any programming language) for like half an hour and I hardly know what I am doing. Does anybody know how to achieve exporting images into a subfolder?
UPDATE:
Now it looks like this
sourcename = pdb.gimp_image_get_filename(ima)
basedir = os.path.dirname(sourcename)
if os.path.isdir(os.path.join(basedir,'Subfolder')) is False:
os.makedirs(os.path.join(basedir,'Subfolder'))
fullpath = os.path.join(basedir,'Subfolder',filename)
... and it works well. Almost. Now I am facing the problem with diacritics in basedir. When basedir contains something like "C:\Ăšklid\" I get "no such file or directory" error when code is creating Subdirecotry in it. After I rename the source folder to "C:\Uklid\" it works with ease. But I do need it to work with every path valid in Windows OS. Can someone help me with that?
UPDATE 2:
Looks like unicode() did the trick:
sourcename = pdb.gimp_image_get_filename(ima)
basedir = os.path.dirname(sourcename)
if os.path.isdir(os.path.join(basedir,'Subfolder')) is False:
os.makedirs(unicode(os.path.join(basedir,'Subfolder')))
fullpath = os.path.join(basedir,'Subfolder',filename)
Try this out:
import os
sourcename = pdb.gimp_image_get_filename(ima) # gets full name of source file
basedir= os.path.dirname(sourcename) # gets path
name = pdb.gimp_layer_get_name(ima.layers[0]) # gets name of active layer
filename = name+'.png'
fullpath = os.path.join(basedir,'subfolder',filename) # use os.path.join to build paths
# new line vvv
os.makedirs(os.path.join(basedir,'subfolder')) # make directory if it doesn't exist
drawable = pdb.gimp_image_active_drawable(ima)
pdb.file_png_save_defaults(ima, drawable, fullpath, filename) # saves output file
couple of days back an infected computer infected my USB drive with a shortcut virus and. I had lots of software in that USB drive i plugged it in my Linux machine and cleaned a lot of files but what the virus did is it created an .exe file in each folder with that folder's name. Each .exe file is capable of infecting another PC. So me with a lot of time in my hand was trying to make a python script that goes to each folder check if a .exe file with that folder name exists delete it and run the function for every sub-folder recursively. but it is not working here what i made so far
#!/usr/bin/python
from __future__ import print_function
import os
import argparse
def deldir(fol):
# changing to the directory
os.chdir(fol)
#Trivial case delte the virus in the root folder
cwd = os.getcwd()
#getting just the name of the folder rather than full path by splitting from "/"
cwd = str(cwd).split('/')[-1]
#if any .exe file exists with that name
if any(str(cwd + '.exe') in s for s in os.listdir('.')):
# delete that file
os.remove(str(cwd + '.exe'))
print('Deleting virus file')
#making a list with all the contents in the directory
dirlist = os.listdir('.')
print(dirlist)
#Looping through the directory that we just made a list of its contents
for i in dirlist:
#checking if it is a directory or not
if os.path.isdir(i):
#changing dir to that directory
os.chdir(i)
#getting the current working directory
cwd = os.getcwd()
#getting just the name of the folder rather than full path by splitting from "/"
cwd = str(cwd).split('/')[-1]
#if any .exe file exists with that name
if any(str(cwd + '.exe') in s for s in os.listdir('.')):
# delete that file
os.remove(str(cwd + '.exe'))
print('Deleting virus file', str(cwd + '.exe'))
#listing filders in current directory
for j in os.listdir('.'):
#passing that subdirectory to the function itself if its a folder
if os.path.isdir(j):
deldir(j)
else:
print('Not a folder so skipping')
def main():
#parsing the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("fol", help="Directory to enter")
args = parser.parse_args()
#passing the command line argument with the folder path to "deldir" function
deldir(args.fol)
if __name__ == "__main__":
main()
Please point me to right direction i have no idea what i am doing wrong. Thanks for reading
Here what I see mistake in your code.
1
os.getcwd -> os.getcwd()
It's a function
2.
You using os.chdir(i) inside the for loop. this will work only first directory in that directory.
3.
The algorithm is wrong, It's not checking the directories level 1,3,5,... for virus.
As Hiro and MYGz's comments. os.walk/os.scandir is easier way to go for recursive directory. but your way is better for practice and learn programming.
I got it done the way i wanted with os.walk() it's really easy that way and that takes away the fun ! :'( . I was hoping to fix my own code but somethings not right. well anyways here's the working code if anyone encounters this type of virus or something.
#!/usr/bin/python
from __future__ import print_function
import os
import argparse
def deldir(fol):
for root, dirs, files in os.walk(fol):
try:
os.remove(str(root + '/' + str(root).split('/')[-1 ] + '.exe'))
except:
print('Does not exists' ,str(root + '/' + str(root).split('/')[-1 ] + '.exe'))
def main():
#parsing the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("fol", help="Directory to enter")
args = parser.parse_args()
#passing the command line argument with the folder path to "deldir" function
deldir(args.fol)
if __name__ == "__main__":
main()
So I made this script in to sort my desktop files into folders:
# Desktop Organization Script
import os
import shutil
userhome = os.path.expanduser('~')
src = userhome + '/Desktop/'
dst = src+ 'org/'
f_exe = [".exe",".lnk"]
f_web = [".php",".html",".css"]
f_images = [".jpg",".jpeg",".png",".ico",".gif",".JPG"]
f_video = [".avi"]
f_music = [".ogg",".wav",".mp3"]
f_document = [".txt",".pdf",".ppt",".pptx",".docx",".doc",".xls"]
f_multimedia = [".max",".m",".ase",".swf",".fla"]
f_zip = [".rar",".zip",".7z"]
f_book = [".epub"]
f_other = [".s2z",".url"]
formats = [f_exe,f_web,f_images,f_music,f_document,f_multimedia,f_zip,f_book,f_other,f_video]
def main():
txtlist = os.listdir(src);
for file in txtlist:
sortFiles(file)
def sortFiles(file):
for group in formats:
for format in group:
if file.endswith(format):
if format in f_exe: directory = "Executables"
if format in f_web: directory = "Web Development"
if format in f_images: directory = "Images"
if format in f_music: directory = "Music"
if format in f_document: directory = "Documents"
if format in f_multimedia: directory = "Multimedia"
if format in f_zip: directory = "RARs and ZIPs"
if format in f_book: directory = "Books"
if format in f_other: directory = "Others"
if format in f_video: directory = "Videos"
if not os.path.exists(dst+directory+"/"):
os.makedirs(dst+directory+"/")
shutil.move(src+file,dst+directory+"/")
main()
Although this does work with most files, sometimes there are files that won't be moved by this script, even if they share extension with files that did move properly. Actually, I am only having problems when moving shortcuts (.lnk extension). Some of them move, some of them do not move. The ones that don't move aren't even returned by the os.listdir() command. Any ideas?
As some *.lnk files are not listed by os.listdir(), I assume that they are not available in the user's desktop directory.
The reason for that might be that those files are stored in the "public desktop", i.e. c:\Users\Public\Desktop, and shared across all users.
Since you are only targeting the current user (userhome = os.path.expanduser('~')), the public Deskop remains "hidden" to your code.