why python is dropping errors after execution is being completed - python

I write the program that penetrate inside the folder and check if folder have sub folders that either contained files or not if there is folders which contained files inside program penetrate into it again and delete all founded files in order to make folders empty and get ready to remove it but after the program being executed i get the following error when there is many folders and files inside the directory
PermissionError: [WinError 5] Access is denied: 'FileLocation\\Folder'
and if the directory name inside that directory has two or more words with space separator between them the program runs well but drop the following error
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'FileLocation\\firstName secondName'
and the codes which I wrote with the only os module are the one that follows
import os
# parent folder
New = "C:\\Users\\HP\\Desktop\\New"
# check if the path exists
if os.path.exists(New):
# looping over all files and directories inside the parent folder
for files in os.listdir(New):
# check if there is directories
if os.path.isdir(New + '\\' + files):
# check if the directories are empty and get ready to remove it
if len(os.listdir(New + '\\' + files)) <= 0:
os.rmdir(New + '\\' + files)
# when directories are not empty
else:
# search for file inside the nested directories
for sub_files in os.listdir(New + '\\' + files):
# remove all the files inside the nested directories
os.remove(New + '\\' + files + "\\" + sub_files)
# remove directories after removing files inside it
os.removedirs(New + '\\' + files)
# check if there is files
if os.path.isfile(New + '\\' + files):
# removing the files inside the parent folder
os.remove(New + '\\' + files)
# removing the entire folder after deleting all files and folders inside it
os.rmdir(New)
else:
print('Folder doesn\'t exist')
while when i wrote the program like the second following codes work well without any logic or runtime error and its codes are the following with the shutil and os modules
import shutil
import os
# parent folder
New = "C:\\Users\\HP\\Desktop\\New"
if os.path.exists(New):
shutil.rmtree(New)
else:
print('Folder doesn\'t exist')
so i would like to know if there is any further configuration about the python errors or any bugs in my codes to fix or any way drops no error at runtime which will be better for this(removing directories which is not empty) thanks

I believe it's this part:
if len(os.listdir(New + '\\' + files)) <= 0:
os.rmdir(New + '\\' + files)
# when directories are not empty
else:
# search for file inside the nested directories
for sub_files in os.listdir(New + '\\' + files):
# remove all the files inside the nested directories
os.remove(New + '\\' + files + "\\" + sub_files)
You have if len(os.listdir(New + '\\' + files)) <= 0: which means if the folder is empty. You also have else:, which is for non-empty folders. But, if in that folder, there is another folder that is also not empty, you cannot call os.remove(New + '\\' + files + "\\" + sub_files) on that, so it throws a PermissionError: [WinError 5] Access is denied error.

Click right button on Pychrarm or CMD whatever you use to run the script and select Run as a System administrator

Related

Move multiple files with certain naming pattern from multiple folders using python

I have folders like this
in each folder, it contains file like this :
i want to move every file with the name "Indeks-Standar-Pencemar-Udara-di-SPKU-Bulan*" in each folder to one designated folder, i try this code but nothing happened
target_folder = r"C:\Users\EVOSYS\Documents\PROJECT-ISPU-DKI-JAKARTA-main\File Gabungan ISPU di SPKU 2010 - 2021" + "\\"
source_folder = r"C:\Users\EVOSYS\Documents\PROJECT-ISPU-DKI-JAKARTA-main" + "\\"
for path, dir, files in os.walk(source_folder):
if files:
for file in files:
if not os.path.isfile(target_folder + file):
if "Indeks-Standar-Pencemar-Udara-di-SPKU-Bulan*" in file:
os.rename(path + '\\' + file, target_folder + file)

Python: copying files group to subdirectories

I'm working with Python and I'd like to copy 3 files of a certain folder
/Users/jake/Desktop/exp to all the subfolders of other sub-directories belonging to the directory /toyspace:
/Users/jake/Desktop/toyspace/1A/AAA
/Users/jake/Desktop/toyspace/1A/BBB
/Users/jake/Desktop/toyspace/1A/CCC
/Users/jake/Desktop/toyspace/2B/AAA
/Users/jake/Desktop/toyspace/2B/BBB
/Users/jake/Desktop/toyspace/2B/CCC
So the subfolders names are the same for all the sub-directories. I wrote something like that:
from distutils.dir_util import copy_tree
def myfunc (source, destination):
fromDirectory = source
toDirectory = destination
copy_tree(fromDirectory, toDirectory)
for subfold in toDirectory:
myfunc(fromDirectory, subfold)
Where source =/Users/jake/Desktop/exp and destination =/Users/jake/Desktop/toyspace, but it returns me an error:
DistutilsFileError: could not create '/motif_list.txt': Read-only file system
Can you help me? Thanks in advance!
Unfortunately I have not used distutils but you can try to automate using the os command as below
import os
def copy_folders(source_dir, destination_dir):
files = os.listdir(source_dir)
for sub_folder1 in os.listdir(destination_dir):
for sub_folder in os.listdir(destination_dir + sub_folder1 + '/'):
for file in files:
os.system('cp ' + source_dir + file + ' ' + destination_dir + sub_folder1 + '/' + sub_folder + '/')
Let me know how it goes :)

os.makedirs wrongly catches extension-less files for directory on macOS

I am trying to create directories with the same name as files. There is a file: readme and it has no extension. It gets caught in the os.makedirs(directory) claiming that the file exists.
source = "Users/me/Desktop/parent"
dirpaths = ['readme', 'index', 'robots']
def func(directory,source=source):
directory = os.path.join(source,directory) #
os.makedirs(directory)
a = [func(directory) for directory in dirpaths]
>>> FileExistsError: [Errno 17] File exists: '/Users/me/Desktop/parent/readme'
I changed the line with # to this:
directory = os.path.join(source,directory+"/")
>>> NotADirectoryError: [Errno 20] Not a directory: '/Users/me/Desktop/parent/readme/'
How can I make the directory when an extension-less file of the same name exists?
Python 3.7.3
Turns out, macOS treats directory and extension-less files as the same. I tried moving a folder named readme to parent but it refused.
os.path.isfile(source + "/" + "readme")
True
os.path.isfile(source + "/" + "readme/")
False
os.path.isdir(source + "/" + "readme/")
False
os.path.isdir(source + "/" + "readme")
False
If there is a difference here, can it be used for creating too?
Directories are only special types of files. Specifically, they are just files where the file mode bits indicate that the file is a directory (see the bitmask stat.S_ISDIR). For example, a directory's mode as an octal number might typically be 0o40755 and a regular file 0o100644.
On most filesystems (including macOS), you may not have a directory and a regular file with the same name within the same directory, nor may you have filename which includes the path separator character. This is in contrast to an object store, such as s3, which is not actually a filesystem.
See for yourself, that the same inode is taken whether you specify a trailing slash or not:
>>> import os
>>> os.makedirs("./example")
>>> os.stat('./example/').st_ino == os.stat('./example').st_ino
True

after run python script to run recursive rename my files has disappeared

I've just run this script to rename my files, adding the 15 first chars of files, but now all the files are disappearead, and i can't find them. i've just run this on a mac
import os
def replace(folder_path, old, new):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_path = os.path.join(path,name)
new_name = os.path.basename(path)[:15] + " - " + name
print path + "##" + new_name
os.rename(file_path, new_name)
replace('/Users/myuser/mp3/', '', '')
#DDave,
In the function call os.rename(src, dst): src is the fully qualified filepath of the source file, and dst is fully qualified filepath of the target file.
In your above program, new_name is just the new name of the file which does not have the directory information (and hence it is considering the directory as your current working directory). That is why you are not seeing the renamed files where you were looking to see them.
The program is rather trying to create the files under your current working directory. Run the following from your console of your IDE or from your program to figure out the current working directory:
print(os.getcwd())
Providing the full solution as requested in the comment below:
import os
def replace(folder_path, old, new):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_path = os.path.join(path,name)
# new_name = os.path.basename(path)[:15] + " - " + name
new_filepath = os.path.join(path, (name[:15] if len(name)>=15 else name) + " - " + name)
print ("new path: " + new_filepath)
os.rename(file_path, new_filepath)

Python: How to copy specific files from one location to another and keep directory structure

Hi I'm struggling with some python code for to copy specific files in a folder to another folder whilst keeping the directory structure.
I'm learning so this code is put together using various code snippets I've found, I couldn't find anything that exactly matched my circumstance and I don't understand python enough yet to understand where I've gone wrong
def filtered_copy(src_dir, dest_dir, filter):
print 'Copying files named ' + filter + ' in ' + src_dir + ' to ' + dest_dir
ignore_func = lambda d, files: [f for f in files if isfile(join(d, f)) and f != filter]
if os.path.exists(dest_dir):
print 'deleting existing data'
shutil.rmtree(dest_dir)
copytree(src_dir, dest_dir, ignore=ignore_func)
Executing this code like this
filtered_copy(c:\foldertosearch, c:\foldertocopyto, 'settings.xml')
does copy across the file I want but does not copy across the parent folder i.e. the src_dir, so the result I'm trying to achieve is:
c:\foldertocopyto\foldertosearch\settings.xml
*** Edit - to clarify this is a script that will be used on multiple operating systems
So if the folder structure was more complex i.e.
Parent folder
-subfolder
--subsubfolder
----subsubsubfolder
------settings.xml
and I ran
filtered_copy(subsubsubfolder, foldertocopyto, 'settings.xml')
I would want the new folder structure to be
foldertocopyto (there could be more parent folders above this or not)
--subsubsubfolder
----settings.xml
In other words the folder I search for a specific file in should also be copied across and if that folder already exists it should be deleted before the folder and file is copied across
I assumed copytree() would do this part - but obviously not!
*** end of Edit
*** Latest code changes
This works but I'm sure it's a long-winded way, also it copies blank folders, presumably because of the copytree() execution, I'd prefer just the folder that's being searched and the filtered file...
def filtered_copy(src_dir, dest_dir, filter):
foldername = os.path.basename(os.path.normpath(src_dir))
print 'Copying files named ' + filter + ' in ' + src_dir + ' to ' + dest_dir + '/' + foldername
ignore_func = lambda d, files: [f for f in files if isfile(join(d, f)) and f != filter]
if os.path.exists(dest_dir + '/' + foldername):
print 'deleting existing data'
shutil.rmtree(dest_dir)
copytree(src_dir, dest_dir + '/' + foldername, ignore=ignore_func)
*** end of latest code changes
You can use distutils.dir_util.copy_tree. It works just fine and you don't have to pass every argument, only src and dst are mandatory.
However in your case you can't use a similar tool like shutil.copytree because it behaves differently: as the destination directory must not exist this function can't be used for overwriting its contents
Give a try to a sample code below:
def recursive_overwrite(src, dest, ignore=None):
if os.path.isdir(src):
if not os.path.isdir(dest):
os.makedirs(dest)
files = os.listdir(src)
if ignore is not None:
ignored = ignore(src, files)
else:
ignored = set()
for f in files:
if f not in ignored:
recursive_overwrite(os.path.join(src, f),
os.path.join(dest, f),
ignore)
else:
shutil.copyfile(src, dest)

Categories

Resources