Shutil.copy2() Operation not permitted error - python

I have created the basic script below that scans a directory for wav files and copies them into a nested directory based on file attributes.
"""
Script that copies data from Peersonic RPA3 into directory
using file name, date and time
"""
import datetime as dt
import os
import shutil
import time
from pathlib import Path
# Function to check if survey is dusk or dawn
def duskdawn(file):
filetime = dt.datetime.fromtimestamp(os.path.getmtime(file))
if file_time.time() > dt.time(12):
return "Dusk"
else:
return "Dawn"
def file_date(file):
return time.strftime('%d.%m.%Y', time.localtime(os.path.getmtime(file)))
def client(file):
return file.name[0:3]
client_lst = ['IES', 'DEC', 'TEP']
# Directory of files
path = "/Volumes/BATrecorder/WAVFILES.DIR"
for entry in os.scandir(path):
source = entry.path
perm = os.stat(entry).st_mode
client_name = (client(entry))
if entry.name.endswith(".WAV"):
if client_name in client_lst:
client_name = client_name
else:
client_name = "unknown"
destination = f"/Volumes/Seagate Exp/Work 2020/Bat recordings/{client_name}/{file_date(entry)}/{duskdawn(entry)}"
Path(destination).mkdir(parents=True, exist_ok=True)
shutil.copy2(source, destination)
Initially I used the standard shutil.copy() function and this worked fine. However, I need to keep the original file metadata so need to use shutil.copy2() instead. When I use this one I get the following error:
Traceback (most recent call last):
File "/Volumes/Seagate Exp/Work 2020/file_copy.py", line 43, in <module>
shutil.copy2(source, destination)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 258, in copy2
copystat(src, dst, follow_symlinks=follow_symlinks)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 218, in copystat
lookup("chflags")(dst, st.st_flags, follow_symlinks=follow)
PermissionError: [Errno 1] Operation not permitted: '/Volumes/Seagate Exp/Work 2020/Bat recordings/unknown/31.12.2014/Dusk/#AAA_000.WAV'
What is going on here that causes this issue?

Related

How can I process only which files have been copied completely?

I am writing a Python script in Windows. I'm processed Zip File from the folder if the file is completed copied and start the program it is working fine, but I encounter with Problem when the program is running and start Copy file in the folder. It's giving error and closed.
How can I process only which files have been copied completely?
Or check if the file size is increasing it does not process in 30 seconds interval if file size does not increase then it processed?
My code:
import zipfile
import os
import xml.etree.ElementTree as ET
import shutil
import configparser
import time
#function used to read zip and xml
def read_zipfile(file):
with zipfile.ZipFile(file,'r') as zf:
for name in zf.namelist():
if name.endswith('.xml'):
#open Zip and read Xml
xml_content=zf.open(name)
# here you do your magic with [f] : parsing, etc
return xml_content
#Function use to parsing XML file
def XMl_Parsa(f):
tree=ET.parse(f)
root=tree.getroot()
# attribute are iterated attribute to get value of tag;
for node in tree.iter('attribute'):
if(node.attrib['name']=='ProductNameCont'):
zone = str(node.text)
return zone
#fucnction used to move file
def move_zipFile(zone,out_put,in_xml,file):
#defing destination path
Product_zone=(os.path.join(out_put,zone))
print(Product_zone)
#Moveing fine for base folder to Product folder
try:
os.makedirs(Product_zone,exist_ok=True)
print("Directory '%s' created successfully" % zone)
except OSError as error:
print("Directory '%s' Exist " % error)
try:
#unziping zip file
shutil.unpack_archive(os.path.join(in_xml, file),os.path.join(Product_zone,os.path.splitext(file)[0]))
os.remove(os.path.join(in_xml, file))
print("File '%s' moved to successfully" % file)
except OSError as error:
print("File '%s' Exist " % error)
#Function use for read Config File
def config_read():
config = configparser.ConfigParser()
config.read('./Config.ini')
xml_path = config.get('Path', 'xml_path')
dest = config.get('Path', 'dest')
return xml_path,dest
def main():
in_xml=config_read()[0]
out_put=config_read()[1]
for file in os.listdir(in_xml):
move_zipFile(XMl_Parsa(read_zipfile(os.path.join(in_xml, file))),out_put,in_xml,file)
if __name__=="__main__":
while 1:
time.sleep(10)
main()
Error
Traceback (most recent call last):
File "Clero_zipFile_transfer - Copy.py", line 65, in <module>
File "Clero_zipFile_transfer - Copy.py", line 60, in main
File "Clero_zipFile_transfer - Copy.py", line 9, in read_zipfile
File "zipfile.py", line 1268, in __init__
File "zipfile.py", line 1335, in _RealGetContents
zipfile.BadZipFile: File is not a zip file
[2916] Failed to execute script Clero_zipFile_transfer - Copy

Decompressing .bz2 files in a directory in python

I would like to decompress a bunch of .bz2 files contained in a folder (where there are also .zst files). What I am doing is the following:
destination_folder = "/destination_folder_path/"
compressed_files_path="/compressedfiles_folder_path/"
dirListing = os.listdir(compressed_files_path)
for file in dirListing:
if ".bz2" in file:
unpackedfile = bz2.BZ2File(file)
data = unpackedfile.read()
open(destination_folder, 'wb').write(data)
But I keep on getting the following error message:
Traceback (most recent call last):
File "mycode.py", line 34, in <module>
unpackedfile = bz2.BZ2File(file)
File ".../miniconda3/lib/python3.9/bz2.py", line 85, in __init__
self._fp = _builtin_open(filename, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'filename.bz2'
Why do I receive this error?
You must be sure that all the file paths you are using exist.
It is better to use the full path to the file being opened.
import os
import bz2
# this path must exist
destination_folder = "/full_path_to/folder/"
compressed_files_path = "/full_path_to_other/folder/"
# get list with filenames (strings)
dirListing = os.listdir(compressed_files_path)
for file in dirListing:
# ^ this is only filename.ext
if ".bz2" in file:
# concatenation of directory path and filename.bz2
existing_file_path = os.path.join(compressed_files_path, file)
# read the file as you want
unpackedfile = bz2.BZ2File(existing_file_path)
data = unpackedfile.read()
new_file_path = os.path.join(destination_folder, file)
with bz2.open(new_file_path, 'wb') as f:
f.write(data)
You can also use the shutil module to copy or move files.
os.path.exists
os.path.join
shutil
bz2 examples

Error when moving files

I'm trying to write a code that moves files in my download folder to other specified folders but I keep getting errors. Here's my code.
import os
import shutil
series = []
for i in os.listdir('C:\\Users\\Mike\\Downloads\\Video'):
if ('.mp4') in i:
series.append(i)
for j in series:
if 'Thrones' in j:
shutil.move(j,'C:\\Users\\Mike\\Desktop\\')
I keep getting this error
Traceback (most recent call last):
File "C:/Users/Mike/Downloads/Video/Arrange.py", line 70, in <module>
Series(series)
File "C:/Users/Mike/Downloads/Video/Arrange.py", line 48, in Series
shutil.move(serie, 'C:\\Users\\Mike\\Desktop\\Movies\\Series\\Lost\\s2\\')
File "C:\Users\Mike\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 536, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path 'C:\Users\Mike\Desktop\Movies\Series\Lost\s2\lost - s02e08 (o2tvseries.com).mp4' already exists
>>>
but the file actually moves. How do I move the files without getting this error each time?
The error you get is Windows platform specific. You are using shutil.move which uses os.rename under the hood. From docs:
On Windows, if dst already exists, OSError will be raised even if it is a file
You could check if the file exists in the destination before you move it and depending what you want to achieve:
1) don't overwrite the destination, just remove file from source
2) remove the file from source first and overwrite the destination
Below you can find implementation of solution 2)
import os
for name in series:
if 'Thrones' in name:
if not os.path.isfile(name):
shutil.move(name, 'C:\\Users\\Mike\\Desktop\\')
else:
os.remove(name)

Copying files into multiple directories using Python Shutil

I am trying to use shutil.copytree to copy a directory onto multiple other directories. I can't get it to work. I'm pretty sure I just need to implement ignore_errors=True, but I cant get it to work. How should I go about implementing 'ignore_errors=True' into
for CopyHere in DeleteThis:
for CopyThis in FilestoCopy:
shutil.copytree(CopyThis, CopyHere)
print('Files have been copied')
My code is as follows:
import shutil
import time
DeleteThis = ['E:', 'F:']
FilestoCopy = ['C:\\Users\\2402neha\\Desktop\\Hehe']
for Directory_to_delete in DeleteThis:
shutil.rmtree(Directory_to_delete, ignore_errors=True)
print('Directories have been wiped')
time.sleep(2)
for CopyHere in DeleteThis:
for CopyThis in FilestoCopy:
shutil.copytree(CopyThis, CopyHere)
print('Files have been copied')
Here are the error messages I get:
Traceback (most recent call last):
File "C:\Users\2402neha\OneDrive\Python\Dis Cleaner\Copy paste test.py", line 17, in <module>
shutil.copytree(CopyThis, CopyHere)
File "C:\Users\2402neha\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 309, in copytree
os.makedirs(dst)
File "C:\Users\2402neha\AppData\Local\Programs\Python\Python35\lib\os.py", line 241, in makedirs
mkdir(name, mode)
PermissionError: [WinError 5] Ingen tilgang: 'E:'
Your destination is E:
The destination directory needs to not exist.
From the documentation for shutil.copytree:
shutil.copytree(src, dst, symlinks=False, ignore=None)
Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories.
You probably want the directory name you're copying and to join it with the destination:
directory = os.path.basename(CopyThis)
destination = os.path.join(CopyHere, directory)
shutil.copytree(CopyThis, destination)

Python script to copy files

I am trying to write a Python script that will copy files from one user’s home directory into another user’s home directory. I want it to copy the permissions, as well. I read the Python API and I think that the copy2 method does just that. However, when I run the following code, I get errors.
import shutil
src = raw_input("Please enter a source: ")
dst = raw_input("Please enter a destination: ")
shutil.copy2(src, dst)
The error says:
Traceback (most recent call last):
File "copyfiles.py", line 5, in <module>
shutil.copy2(src, dst)
File "/usr/lib/python2.6/shutil.py", line 99, in copy2
copyfile(src, dst)
File "/usr/lib/python2.6/shutil.py", line 52, in copyfile
fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: '../../../../Desktop/byteswap.c'
Check your current directory using os.getcwd().

Categories