I need to prepare script which user backups in Python (I'm the beginner in Python) and I have a problem with copy files beetwen catalogs. When I run script, I get this error:
*PermissionError: [Errno 13] Permission denied: 'C:/Users/User/Documents/PythonProjects\\New catalog'*
New catalog is a dir which is in PythonProjects dir.
What I did:
I checked permissions in catalog PythonProjects (User has persmissions to read/save/modify this catalog). I dont' know what I can to do next step to verify this issue. I saw many threads and any solution doesn't fix my issue.
This is my code:
import shutil
import os
sourceCatalog = "C:/Users/User/Documents/PythonProjects"
destinationCatalog = "D:/BACKUP_NEW"
files = os.listdir(sourceCatalog)
for filesNames in files:
shutil.copy2(os.path.join(sourceCatalog, filesNames), destinationCatalog)
Thank you for potential tips.
Python's shutil module offers several functions to copy files or directories. As opposed to the cp or copy commands, shutil.copy[2] however, is not a one size fits all solution.
Namely, shutil.copy2 will refuse to copy a directory and give the "permission denied" error (at least on Windows). To copy a directory - or rather an entire directory tree - shutil.copytree is the right command.
So the fixed code reads:
import shutil
import os
sourceCatalog = "C:/Users/User/Documents/PythonProjects"
destinationCatalog = "D:/BACKUP_NEW"
files = os.listdir(sourceCatalog)
for filesNames in files:
shutil.copytree(os.path.join(sourceCatalog, filesNames), destinationCatalog)
Related
I am trying to run a Streamlit app importing pickle files and a DataFrame. The pathfile for my script is :
/Users/myname/Documents/Master2/Python/Final_Project/streamlit_app.py
And the one for my DataFrame is:
/Users/myname/Documents/Master2/Python/Final_Project/data/metabolic_syndrome.csv
One could reasonably argue that I only need to specify df = pd.read_csv('data/df.csv') yet it does not work as the Streamlit app is unexpectedly not searching in its directory:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/myname/data/metabolic_syndrome.csv'
How can I manage to make the app look for the files in the good directory (the one where it is saved) without having to use absolute pathfiles ?
you can use os.getcwd() to get the Current Working Directory. Read up on what this means exactly, fe here
See the sample script below on how to use it. I'm using os.sep for OS agnostic filepath separators.
import os
print(os.getcwd())
relative_path = "data"
full_path = f"{os.getcwd()}{os.sep}{relative_path}"
print(full_path)
filename = "somefile.csv"
full_file_path = f"{full_path}{os.sep}{filename}"
print(full_file_path)
with open(full_file_path) as infile:
for line in infile.read().splitlines():
print(line)
In which directory are you standing when you are running your code?
From your error message I would assume that you are standing in /Users/myname/ which makes python look for data as a subdirectory of /Users/myname/.
But if you first change directory to /Users/myname/Documents/Master2/Python/Final_Project and then run your code from there I think it would work.
I have tried to use shutil module in python. The shutil.copytree fails to copy folders and brings FileExistsError. I have tried it using different files but it still fails.Please check
import shutil
from pathlib import Path
p = Path(r"C:\Users\rapid\OneDrive\Desktop")
shutil.copytree(p/"folder_1", p/"folder_2")
Please check for the error image
I tried the code myself and works fine the first time.
After the destination folder (in your case folder_2) already created shutil can't create it again and it fails. So you can either delete the destination folder manually or using shutil.rmtree.
Good Luck!
All credit to Timus. He provided the solution on the comment section. Just used
shutil.copytree(p/"folder_1", p/"folder_2", dirs_exist_ok=True)
It works fine. Thanks Timux
Currently, I have a folder structure as below in my python project and I wanted to open the sample.json file in the run.py file.
parent_folder
--subfolder1
--sub_sub_folder1
--sub_sub_sub_folder1
--run.py
-- sub_sub_folder2
--sample.json
So I have tried as below
file_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),'../../sub_sub_folder2/sample.json')
file_content= open(file_dir)
But I am getting error as below
[Errno 2] No such file or directory: '/usr/local/lib/python3.6/site-packages/sub_sub_folder2/sample.json'
Could some please help me?
You can first change the directory into where your python program exists through os.chdir:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
And then, specify the path of your target file:
file_dir = '../../sub_sub_folder2/sample.json'
Your way to do it seems ugly to me, but it should work. Are you sure, the folder structure is as you describe it? The ErrorMessage implies that the parent folder of your project is /usr/local/lib/python3.6/ - that most probably not true.
A cleaner way would make use of pathlib
from pathlib import Path
path_script = Path(__file__)
path_ancestor_common = path_script.parents[2]
path_json = path_ancestor_common.joinpath(
'sub_sub_folder2', 'sample.json')
I've been working on a project where I need to import csv files, previously the csv files have been in the same working directory. Now the project is getting bigger so for security and organizational resaons I'd much prefer to keep them in a different directory.
I've had a look at some other questions asking similar things but I couldn't figure out out how to apply them to my code as each time I tried I kept getting the same error message mainly:
IOError: [Errno 2] No such file or directory:
My original attempts all looked something like this:
import csv # Import the csv module
import MySQLdb # Import MySQLdb module
def connect():
login = csv.reader(file('/~/Projects/bmm_private/login_test.txt'))
I changed the path within several times as well by dropping the first / then then the ~ then the / again after that, but each time I got the error message. I then tried another method suggested by several people by importing the os:
import os
import csv # Import the csv module
import MySQLdb # Import MySQLdb module
def connect():
path = r'F:\Projects\bmm_private\login_test.txt'
f = os.path.normpath(path)
login = csv.reader(file(f))
But I got the error message yet again.
Any help here would be much appreciated, if I could request that you use the real path (~/Projects/bmm_private/login_test.txt) in any answers you know of so it's very clear to me what I'm missing out here.
I'm pretty new to python so I may struggle to understand without extra clarity/explanation. Thanks in advance!
The tilde tells me that this is the home folder (e.g. C:\Users\<username> on my Windows system, or /Users/<username> on my Mac). If you want to expand the user, use the os.path.expanduser call:
full_path = os.path.expanduser('~/Projects/bmm_private/login_test.txt')
# Now you can open it
Another approach is to seek for the file in relative to your current script. For example, if your script is in ~/Projects/my_scripts, then:
script_dir = os.path.dirname(__file__) # Script directory
full_path = os.path.join(script_dir, '../bmm_private/login_test.txt')
# Now use full_path
I am sure I am missing this, as it has to be easy, but I have looked in Google, The Docs, and SO and I can not find a simple way to copy a bunch of directories into another directory that already exists from Python 3?
All of the answers I find recommend using shutil.copytree but as I said, I need to copy a bunch of folders, and all their contents, into an existing folder (and keep any folders or that already existed in the destination)
Is there a way to do this on windows?
I would look into using the os module built into python. Specifically os.walk which returns all the files and subdirectories within the directory you point it to. I will not just tell you exactly how to do it, however here is some sample code that i use to backup my files. I use the os and zipfile modules.
import zipfile
import time
import os
def main():
date = time.strftime("%m.%d.%Y")
zf = zipfile.ZipFile('/media/morpheous/PORTEUS/' + date, 'w')
for root, j, files in os.walk('/home/morpheous/Documents'):
for i in files:
zf.write(os.path.join(root, i))
zf.close()
Hope this helps. Also it should be pointed out that i do this in linux, however with a few changes to the file paths it should work the same