In this program I'm checking to see if a directory has been made, if it is then I will delete the directory and make a new one with the same name. If not then the program will carry on to the next step.
import shutil
import os
try:
os.mkdir('Result')
except:
shutil.rmtree('Result')
os.mkdir('Result')
Here I am simply using shutil to remove the directory and then os to add the new one.
>>> os.mkdir('Result')
>>> FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'Result'
However, I don't understand why I am receiving this problem, on some tries the program will work flawlessly with no errors, then on the next it will report back the same error.
The reason as to why I am deleting the folder is because it contains files that I no longer need, new files need to be added and if they are interfered with the program will become corrupt. These files will generally have the same name.
EDIT:
A Fix has been found as I needed to give permission when writing a new directory
os.mkdir('Result', 0o777)
Related
I had a quick google of this but couldn't find anything. I'm using os to get a list of all the file names in the current working directory using the following code:
path = os.getcwd()
files = os.listdir(path)
The list of files returns fine, but the last element has an extra '~$' that isn't in the actual file name. For example:
files
['File1.xlsx', 'File2.xlsx', '~$File3.xlsx']
This is then causing an issue when I iterate through these files to try and import them, as I get the error of:
[Errno 2] No such file or directory: 'C:\\Users\\$File3.xlsx'
If anyone knows why this happens and how I can fix/prevent it, that would be great!
Just thought I'd answer in case anyone else has this issue.
It's nothing to do with os. It happened because I had File3 open in Excel while pulling the list of file names. I've found out that opening a microsoft document creates a temporary 'lock' file, which are denoted by '~$' (this is how it can re-open unsaved data if it crashes etc).
I found the below from here:
The files you are describing are so-called owner files (sometimes
referred to as "lock" files). An owner file is created when you work
with a document ... and it should be deleted when you save your
document and exit.
There's also a SO question about this within Microsoft files, which can be found here
I'm just trying to rename a file or move it to another directory. Either option will work for me. I have the most basic code here that I could find on the internet (first python project here by the way).
import shutil
import os
while True:
for file in os.listdir("C:/Users/lines/OneDrive/Desktop/loc1"):
if file.endswith(".txt"):
shutil.move("C:/Users/lines/OneDrive/Desktop/loc1/" + file, "C:/Users/lines/OneDrive/Desktop/loc2/moved.txt")
# os.rename("C:/Users/lines/OneDrive/Desktop/loc1/" + file, "C:/Users/lines/OneDrive/Desktop/loc2/moved.txt")
But no matter what I try, i ALWAYS get this error in my console when running the file:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/lines/OneDrive/Desktop/loc1/i000130126543220150615123030.txt' -> 'C:/Users/lines/OneDrive/Desktop/loc2/moved.txt'
I truly have no idea how to fix this. Can anyone help?
Edit: both directories are totally empty and only being used for this test purpose. No other text editors have these files open anywhere.
In my code, I create a directory like so:
try:
os.makedirs(playlist_name)
except OSError as e:
if e.errno != errno.EEXIST:
raise
Which creates a directory in the place where I run my python script.
Then I wish to copy three files from the original directory where the folder is located into the newly created directory, like so
# Copy FFMPEG files into that folder so youtube dl can download the videos as audio tracks
# Tried using os.getcwd() to get full path, same error
shutil.copyfile(os.getcwd() + '\\ffmpeg.exe', os.getcwd() + "\\" + playlist_name)
shutil.copyfile('ffplay.exe', "/" + playlist_name + "/")
shutil.copyfile('ffprobe.exe', "/" + playlist_name + "/")
However, trying to copy those files throws this error:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\ME\\Documents\\python\\DIRECTORY\\PLAYLIST_NAME_HERE'
I have tried various shutil copy methods with the same error.
EDIT: This is running on windows
Per the copyfile docs:
dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path.
You can't use it to do what you do in the shell, naming a source file and a target directory and having it deduce the file should be put in the directory with the file's original name. You have to explicitly name the target file, or it thinks you're trying to copy to the same name as the directory, and unlike replacing a file, you can't replace a directory with a file without explicitly renaming the directory or deleting the whole directory tree first. To fix, just make sure to repeat the file name in both source and destination:
for filename in ('ffmpeg.exe', 'ffplay.exe', 'ffprobe.exe'):
shutil.copyfile(filename, os.path.join(playlist_name, filename))
The problem would be more obvious on a UNIX-like system, because those systems would reject the action with EISDIR, causing a Python IsADirectoryError to be raised, but Windows for some reason chose to use more general error codes associated with permission/access problems (EACCES and related Windows specific error codes) that Python translates to PermissionError (because Windows just isn't telling it the real problem, and it would introduce all sorts of race conditions if Python tried to check if the real problem was trying to use a directory as a file to fix the exception type).
So I am trying to make my very first python program to automate a task that I have. The first snippet of code is from a python script that makes a new folder at a pre-specified destination and then moves files from their original location to the new folder. This part works. The folder is created like so:
os.makedirs(new_folder, 0o777)
new_folder stores the name, given by the user, of the folder to be created.
The next snippet of code is from another script that does the opposite. It takes the files from the new folder and moves them back to the original folder and it does this successfully. However what doesn't work is what is supposed to happen next. Once moved back, it is supposed to delete the new folder with its content. I tried doing it with this code:
os.chdir(new_path)
os.remove(folder_name)
og_path is just a variable that stores the path of the new folder which should be deleted. folder_name stores well...the folder's name
When I run the full code of the second script everything works; however when it reaches:
os.remove(folder_name)
It gives me this error:
Traceback (most recent call last):
File "/Users/TVM/Desktop/python/move_file/move_file_reverse.py", line 25, in <module>
os.remove(folder_name)
PermissionError: [Errno 1] Operation not permitted: 'lab3'
Additional Variable Information:
new_folder = "lab3"
folder_name = "lab3"
new_path = "/Users/TVM/Desktop/python/move_file/newloc"
The folder called lab3 is in the folder newloc
You should use os.rmdir instead of os.remove.
os.mkdir('mydir')
os.rmdir('mydir')
os.path.exists('mydir')
In case if the directory is not empty and you would like to get rid of the whole directory tree starting from the directory you should use:
shutil.rmtree('mydir')
In the comments #ShadowRanger suggest to use shutil.rmtree()
I replaced os.remove() with shutil.rmtree() and it worked. Thank you very much #ShadowRanger.
I am trying to make a function that copies all the directories and files and puts them in a folder. This directory does not have a backup folder, so the first time I make one using copy tree, and each time you re-run this program, it should back up again (including the prevrious backup). I am having a problem with copytree getting caught in a huge recursive loop and don't know what I am doing wrong. Here is my code. It works for the first time but when I run it a second time it messes up. argv[1] is a local folder you wish to backup. This would be run as:
% python3 filename foldername
from os import path, walk, remove, mkdir, listdir
from sys import argv
from shutil import copytree
if path.exists(argv[1]+'/Versions'):
copytree(argv[1], argv[1]+ '/Versions/' + str((len(listdir(argv[1]+'/Versions')))+1))
else:
copytree(argv[1], argv[1]+'/Versions/1')
If the Versions folder is already there, it counts the number of subfolders and creates a new folder with its label +1 the number of folders present
It looks to me you're creating your backup in a subfolder of the folder you're backing up.
So the next time you run your script, you're making a backup of a backup, and then a backup of a backup of a backup and so on
Put your backup into a location that isn't a subfolder of your original data, and your script should work.
source_path = os.path.join(argv[1], '/Versions')
destination_path = os.path.join(argv[1], '..', '/Backup')
#Now handle copying between these two locations
...
Using Ignore method
Alternatively, you can pass in a callable to the copytree to ignore certain directories.
from shutil import copytree, ignore_patterns
copytree(source_path, destination_path, ignore=ignore_patterns('Versions'))