My program works sometimes and sometimes it dont - python-3.x

I have created a simple script to sort things out in a folder but when i run it sometimes it does everything fine. I am using linux. I have run it in an folder for testing. It does everthing fine.
But sometime, it give me error :
Traceback (most recent call last):
File "/mnt/Linux Data/FolderSorter/main.py", line 49, in <module>
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"Documents/",file))
FileNotFoundError: [Errno 2] No such file or directory: './document.docx' -> './Documents/document.docx'
I have written a very short code. It is very simple.
It makes a list with files. Then, I run for loop to go through from everyfile and check its type and move it to the specific directory.
So my code is :
#!/usr/bin/env python
import os # For dealing with folders and files
print("Welcome to Folder Sorter!")
path_of_folder = str(input("Enter the path of folder to be sorted:\n"))
def createFolderIfNotExists(folder_path):
print(folder_path)
if not os.path.exists(folder_path):
os.mkdir(folder_path)
print(f"Created {folder_path}")
#File Extensions lists
audio_extensions = ['.mp3','.wav','.aif','.mid','.wproj','.weba','.flp','.pek','.sdt']
video_extensions = ['.mp4','.avi','.mpv','.webm','.swf','.aep','.mkv']
documents_extensions = ['.docx','.doc','.xlsx','.xlx','.xls','.ppt','.pptx','.odf','.txt']
pictures_extensions = ['.png','.jpg','.jpeg','.svg']
icons_extensions = ['.ico']
ebooks_extensions = ['.pdf','.epub','.azw','.lit']
# print(os.path.join(path_of_folder,"Audios/"))
files = os.listdir(path_of_folder)
for file in files:
print("The current process is:", file)
for ext in audio_extensions:
if ext in file:
createFolderIfNotExists(os.path.join(path_of_folder,"Audios/"))
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"Audios/",file))
files.remove(file)
else:
pass
for ext in video_extensions:
if ext in file:
createFolderIfNotExists(os.path.join(path_of_folder,"Videos/"))
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"Videos/",file))
files.remove(file)
else : pass
for ext in documents_extensions:
if ext in file:
createFolderIfNotExists(os.path.join(path_of_folder,"Documents/"))
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"Documents/",file))
files.remove(file)
else : pass
for ext in pictures_extensions:
if ext in file:
createFolderIfNotExists(os.path.join(path_of_folder,"Pictures/"))
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"Pictures/",file))
files.remove(file)
else : pass
for ext in icons_extensions:
if ext in file:
createFolderIfNotExists(os.path.join(path_of_folder,"Icons/"))
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"Icons/",file))
files.remove(file)
else : pass
for ext in ebooks_extensions:
if ext in file:
createFolderIfNotExists(os.path.join(path_of_folder,"EBooks/"))
os.rename(os.path.join(path_of_folder,file),os.path.join(path_of_folder,"EBooks/",file))
files.remove(file)
else : pass
print("Sorted Successfully!!!!")
Thanks in advance

Related

Python Program error - The process cannot access the file because it is being used by another process

I am trying to test a python code which moves file from source path to target path . The test is done using pytest in Python3 . But I am facing a roadblock here. It is that , I am trying to remove the source and target paths at end of code completion. For this I am using a command like shutil.rmtree(path) or os.rmdir(path) . This is causing me the error - " [WinError 32] The process cannot access the file because it is being used by another process". Please help me on this. Below is the python pytest code :
import pytest
import os
import shutil
import tempfile
from sample_test_module import TestCondition
object_test_condition = TestCondition()
#pytest.mark.parametrize("test_value",['0'])
def test_condition_pass(test_value):
temp_dir = tempfile.mkdtemp()
temp_src_folder = 'ABC_File'
temp_src_dir = os.path.join(temp_dir , temp_src_folder)
temp_file_name = 'Sample_Test.txt'
temp_file_path = os.path.join(temp_src_dir , temp_file_name)
os.chdir(temp_dir)
os.mkdir(temp_src_folder)
try:
with open(temp_file_path , "w") as tmp:
tmp.write("Hello-World\n")
tmp.write("Hi-All\n")
except IOError:
print("Error has occured , please check it.")
org_val = object_test_condition.sample_test(temp_dir)
print("Temp file path is : " + temp_file_path)
print("Temp Dir is : " + temp_dir)
shutil.rmtree(temp_dir)
print("The respective dir path is now removed.)
assert org_val == test_value
Upon execution of the code , the below error is popping up :
[WinError32] The process cannot access the file because it is being used by another process : 'C:\Users\xyz\AppData\Local\Temp\tmptryggg56'
You are getting this error because the directory you are trying to remove is the current directory of the process. If you save the current directory before calling os.chdir (using os.getcwd()), and chdir back to that directory before removing temp_dir, it should work.
Your code isn't correctly indented, so here is my best guess at what it should look like.
import pytest
import os
import shutil
import tempfile
from sample_test_module import TestCondition
object_test_condition = TestCondition()
#pytest.mark.parametrize("test_value",['0'])
def test_condition_pass(test_value):
temp_dir = tempfile.mkdtemp()
temp_src_folder = 'ABC_File'
temp_src_dir = os.path.join(temp_dir , temp_src_folder)
temp_file_name = 'Sample_Test.txt'
temp_file_path = os.path.join(temp_src_dir , temp_file_name)
prev_dir = os.getcwd()
os.chdir(temp_dir)
os.mkdir(temp_src_folder)
try:
with open(temp_file_path , "w") as tmp:
tmp.write("Hello-World\n")
tmp.write("Hi-All\n")
except IOError:
print("Error has occured , please check it.")
org_val = object_test_condition.sample_test(temp_dir)
print("Temp file path is : " + temp_file_path)
print("Temp Dir is : " + temp_dir)
os.chdir(prev_dir)
shutil.rmtree(temp_dir)
print("The respective dir path is now removed.)
assert org_val == test_value
Can you try to close the temp file before removing
temp.close()

pyinstaller stops python from reading from script directory

I have this block of code that works in python script form but when I package the script to an exe using pyinstaller it always results in the program saying the config file can't be found. I put the config.ini in the same folder as the exe file.
config = configparser.ConfigParser()
configComplete = True
configExists = False
try:
open(os.path.join(sys.path[0],'config.ini'))
config.read(os.path.join(sys.path[0],'config.ini'))
destination = config['server']['ServerAddress']
key = config['server']['ApiKey']
configExists = True
except KeyError:
configComplete = False
except FileNotFoundError:
try:
open(expanduser('~/.config/octoprint-cli.ini'))
config.read(expanduser('~/.config/octoprint-cli.ini'))
destination = config['server']['ServerAddress']
key = config['server']['ApiKey']
configExists = True
except KeyError:
configComplete = False
except FileNotFoundError:
pass
I don't have python currently installed to test this on my machine, but typically when I am looking for a file that's relative to the python file location it's preferable to use:
import os
CONFIG_FILE_PATH = f"{os.path.dirname(__file__)}{os.sep}config.ini"
if os.path.exists(CONFIG_FILE_PATH): # If the file already exists
config.read(CONFIG_FILE_PATH) # Read it
else: # If a config file does not exist
# Either throw error or create fresh config
This code is an OS agnostic way of looking for a file in the same directory as the python file and the catching doesn't necessarily throw an error unless you want it to.
See if that works with pyinstaller, as I believe when I last used it this worked.

adding creation time to a files filename

So far I have the following:
source_folder = 'file_location'
for file in os.listdir(source_folder):
if file.startswith('stnet_'):
os.rename(file, file.replace('stnet_a_b', '%s_' % time.ctime(os.path.getctime(file)) + 'stnet_a_b'))
The issue with is is I keep getting FileNotFoundError: [WinError 2] The system cannot find the file specified 'stnet_a_b.raw'
Can someone point out what I'm doing wrong?
Thanks.
os.listdir can only get the filenames without directory, while os.rename, os.path.getctime needs full name with directory(if your current directory is not conincidently file_location then the file will not be found).
You can use os.path.join to get the full name. And if you are on Windows you must make sure filename doesn't contain special characters which your code contains.
dir = r'file_location'
# os.chdir(dir) # in case you don't want to use os.path.join
for filename in os.listdir(dir):
print(filename)
if filename.startswith('stnet_'):
src = os.path.join(dir, filename)
ctime_str = str(time.ctime(os.path.getctime(src)))
ctime_str = ctime_str.replace(':', '').replace(' ', '') # remove special characters
fn_new = filename.replace('stnet_a_b',
'{}_'.format(ctime_str + 'stnet_a_b'))
des = os.path.join(dir, fn_new)
print('src={}, des={}'.format(src, des))
os.rename(src, des)
please try above code.

Python: how to move through folder recursively and shutil.move() files to target folders

Beginner here. I want to be able to traverse folders and their subdirectorys and files and move all unique file extensions into a dedicated folder for that filetype. Ex .jpg -> into jpg folder. (This is all in Python's IDLE)
I have this code:
os.chdir('c:\\users\\dchrie504\\Downloads_2')
# walk through all files and folders to get unique filetypes.
l_Files = os.walk('c:\\users\\dchrie504\\Downloads_2')
fileTypes = []
for walk in l_Files:
for file in walk[-1]:
fileTypes.append(file.split('.')[-1])
# make all items lowercase to create distinct values
fileTypes = [x.lower() for x in fileTypes]
# get rid of duplicate filetypes by creating set then back to list.
fileTypes = set(fileTypes)
fileTypes = list(fileTypes)
# create a folder for each unique filetype.
for ft in fileTypes:
os.mkdir(os.getcwd() + '\\' + ft)
fileWalk = os.walk('c:\\users\\dchrie504\\Downloads_2')
#Trying to move files to their respective fileType folder.
for fileType in fileTypes:
for folder, sub, files in os.walk('c:\\users\\dchrie504\\Downloads_2'):
for file in files:
if file.endswith('.' + fileType):
shutil.move(file, (os.getcwd() + '\\' + fileType))
Problem is I get the following error when this part executes:
for file in files:
if file.endswith('.' + fileType):
shutil.move(file, (os.getcwd() + '\\' + fileType))
Error Message: Traceback (most recent call last):
File "", line 5, in
shutil.move(file, (os.getcwd() + '\' + fileType))
File "C:\Users\dchrie504\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 555, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path 'c:\users\dchrie504\Downloads_2\txt\New Text Document.txt' already exists
you have to give the full path so shutil.move will overwrite.
shutil.move(fullpathorigin, fullpathdestination)

Adding timestamp to a file in PYTHON

I can able to rename a file without any problem/error using os.rename().
But the moment I tried to rename a file with timestamp adding to it, it throws win3 error or win123 error, tried all combinations but no luck, Could anyone help.
Successfully Ran Code :
#!/usr/bin/python
import datetime
import os
import shutil
import json
import re
maindir = "F:/Protocols/"
os.chdir(maindir)
maindir = os.getcwd()
print("Working Directory : "+maindir)
path_4_all_iter = os.path.abspath("all_iteration.txt")
now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d_%H:%M:%S"))
print(type(timestamp))
archive_name = "all_iteration_"+timestamp+".txt"
print(archive_name)
print(os.getcwd())
if os.path.exists("all_iteration.txt"):
print("File Exists")
os.rename(path_4_all_iter, "F:/Protocols/archive/archive.txt")
print(os.listdir("F:/Protocols/archive/"))
print(os.path.abspath("all_iteration.txt"))
Log :
E:\python.exe C:/Users/SPAR/PycharmProjects/Sample/debug.py
Working Directory : F:\Protocols
<class 'str'>
all_iteration_20180409_20:25:51.txt
F:\Protocols
File Exists
['archive.txt']
F:\Protocols\all_iteration.txt
Process finished with exit code 0
Error Code :
print(os.getcwd())
if os.path.exists("all_iteration.txt"):
print("File Exists")
os.rename(path_4_all_iter, "F:/Protocols/archive/"+archive_name)
print(os.listdir("F:/Protocols/archive/"))
print(os.path.abspath("all_iteration.txt"))
Error LOG:
E:\python.exe C:/Users/SPAR/PycharmProjects/Sample/debug.py
Traceback (most recent call last):
Working Directory : F:\Protocols
<class 'str'>
File "C:/Users/SPAR/PycharmProjects/Sample/debug.py", line 22, in <module>
all_iteration_20180409_20:31:16.txt
F:\Protocols
os.rename(path_4_all_iter, "F:/Protocols/archive/"+archive_name)
File Exists
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'F:\\Protocols\\all_iteration.txt' -> 'F:/Protocols/archive/all_iteration_20180409_20:31:16.txt'
Process finished with exit code 1
Your timestamp format has colons in it, which are not allowed in Windows filenames. See this answer on that subject:
How to get a file in Windows with a colon in the filename?
If you change your timestamp format to something like:
timestamp = str(now.strftime("%Y%m%d_%H-%M-%S"))
it should work.
You can't have : characters as part of the filename, so change
timestamp = str(now.strftime("%Y%m%d_%H:%M:%S"))
to
timestamp = str(now.strftime("%Y%m%d_%H%M%S"))
and you'll be able to rename your file.

Resources