Function cannot find most recent .zip file in Downloads folder - python

Trying to write a program and part of it is to find the most recent zip file in the Downloads folder. Wrote a function and when I attempt to call it, I get an error that says it cannot find the file specified, yet it still references the most recent zip file in my downloads folder:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'CaseBuilderHistReport.1.31.2023.03.15.16.zip'
`def copy_latestfile():
#Get downloads folder path
#downloads_folder_path = os.path.expanduser('~/Downloads')
downloads_folder_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Downloads')
#Define the copy location
copy_location = '/Documents/Python/UCX Portal - Walmart/Data'
#Get list of files in the download folder
files_in_dir = os.listdir(downloads_folder_path)
#Get only zip files from download folder
zip_files = [file for file in files_in_dir if file.endswith('.zip')]
#sort the list of files based on the date the file was created
sort_files = sorted(zip_files,key=os.path.getctime)
#get the most recent file and store it in a variable
latest_file = max(zip_files)
#copy the most recent file to the destined working location
shutil.copy(latest_file)
`

Related

Iterating and modifying files in a directory

I already have code that works to modify one .edi file (testedifact.edi) in the same directory as my program.
however I need to run my script against a folder containing many of these .edi files so I basically want to use my code to be applied to every single file
here's what I have that works on one file:
segmentsNew = []
global segments
with open( "testedifact.edi" , "r+") as edifactile:
segments = edifactile.readlines()
versionNumber = getVersionNumber(segments)
for segment in segments:
#do stuffs
edifactile.close()
with open ("testedifact.edi" , "w") as edifactfile:
edifactile.writelines(segmentsNew)
edifactfile.close()
but I want to be able to do this for files outside of this directory and also on our network drives..
I've tried iterating through the files in my directory (as a little test) and passing every file to "with open.." like this
directory = os.listdir(r'C:\Users\name\test_edi_dir')
for file in directory:
print("printing file names:", file)
with open(file, 'r') as edifactfile:
pass
print(edifactfile.closed)
and I keep getting FileNotFoundError: [Errno 2] No such file or directory: 'testedifact - Kopie (10).edi' though it prints the file name.. what am I doing wrong?
could someone help please?
file only contains the file-name, not the path it is stored in. You need to pass this, too.
path = r'C:\Users\name\test_edi_dir/'
directory = os.listdir(path)
for file in directory:
print("printing file names:", file)
with open(path+file, 'r') as edifactile:
pass
That happens because when you call open(file, 'r') it tries to open a file in the current working directory.
Change your code to this:
directory = os.listdir(r'C:\Users\name\test_edi_dir')
for file in directory:
print("printing file names:", file)
with open('C:\Users\name\test_edi_dir\' + file, 'r') as edifactile:
pass
print(edifactfile.closed)
The next issue is that some files will be actually a directories, and your code may fail with the following error:
traceback (most recent call last):
File "<stdin>", line 3, in <module>
IOError: [Errno 21] Is a directory: '...'
So you want to check if file is actually a file, before opening it:
isFile = os.path.isfile('C:\Users\name\test_edi_dir\' + file)
And finally a complete code is:
directory = os.listdir(r'C:\Users\name\test_edi_dir')
for file in directory:
print("printing file names:", file)
full_filename = 'C:\Users\name\test_edi_dir\' + file
if os.path.isdir(full_filename):
continue
with open(full_filename, 'r') as edifactile:
pass
print(edifactfile.closed)
Looks like you have to pass the entire image path:
with open('C:\Users\name\test_edi_dir\' + file, 'r') as edifactile:
pass

How to open an hdf5 not in library root

I can't open hdf5 files using h5py.File() function unless the files are in the root folder, and I was wondering if there's anyway to change this.
I've already been able to open up the files in the root folder this same way but as soon as a file goes inside of another folder within the root, it throws an error. I've also tried opening it with the path and it didn't work.
# string together file name for field data
file = 'magnets-l2_current-' + date_time
# import file
m = h5py.File(file, 'r')
OSError: Unable to open file (unable to open file: name = 'magnets-l2_current-20190506-102013.hdf5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
This is the error it throws. Again, I have my venv folder as the root, and I can open any data files that are in that folder. But any files inside venv/Data/... I cannot open.

How to open a folder loop through opening other files within that folder in python

This current question is building on from this question.
I am trying to create a python script that will loop through all the text files in the specified folder. The text files contain directories to files that will be moved to a different specified folder. When looping through a text file, it takes the file from the file directory on each line of that text file.
The end goal is to have all the files which are referenced in the text file to move into one specified folder (\1855).
import shutil
dst = r"C:/Users/Aydan/Desktop/1855"
with open(r'C:\Users\Aydan\Desktop\RTHPython\Years') as my_folder:
for filename in my_folder:
text_file_name = filename.strip()
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = r'C:\Users\Aydan\Desktop' + file_name
shutil.move(src, dst)
One text file (1855.txt) contains:
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0002_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0003_1.txt
and another text file (1856.txt) contains:
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0004_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0005_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0006_1.txt
This is the error I get when I run the above script:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
with open(r'C:\Users\Aydan\Desktop\RTHPython\Years') as my_folder:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Aydan\\Desktop\\RTHPython\\Years'
This script doesn't seem to be moving the files named here to the C:/Users/Aydan/Desktop/1855 destination, even though in the script I'm trying to follow the same logic of iterating through each item in the text file, but applying that logic to a folder instead of inside text file.
Any help to find a solution would be brilliant! If you need any more info about the files just ask.
Thanks!
Aydan.
Since you can't open whole folders with the open method, you can get cycle through every .txt file in that folder like that:
import shutil
import glob
dst = r"C:/Users/Aydan/Desktop/1855"
for filename in glob.glob(r"C:\Users\Aydan\Desktop\RTHPython\Years\*.txt"):
text_file_name = filename.strip()
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = r'C:\Users\Aydan\Desktop' + file_name
shutil.move(src, dst)

Trying to pull files from mapped network drive Python

I am trying to pull folder names from a network drive in Python. For example, if I have a mapped drive U:, I want to go through and grab all the folders in folder "example" from the path \emily\hello\example.
This is what I have tried so far.
import os
os.chdir('U:')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join('\emily\hello\example', dirs)
os.chdir(dir)
current = os.getcwd()
new = str(current).split("\")[4]
print(new)
this causes many errors, and I am having an issue with the syntax of a shared drive on the network vs a folder locally on my computer.
I want to see them in a list so i can read line by line and compare this list to another list.
ps. i don't want the files in the folders, just the folder names in the folder
thanks~!
error message is C:\Users\212582086\AppData\Local\Continuum\Anaconda3\python.exe "C:/Users/212582086/Desktop/Vendor sort/main"
Traceback (most recent call last):
File "C:/Users/212582086/Desktop/Vendor sort/main", line 6, in
os.chdir(dir)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\emily\hello\example\Software Archive'
Process finished with exit code 1

Copy files from external Hard Drive to local disk

I am trying to write a program to copy user profile data from an attached HD to the local HD. This would copy from a person's old computer to new computer, so all the directories already exist. I am using the dir_util.copy_tree because it will copy the folder data to an already existing destination path. In this case the local disk is C and the attached disk is F. When I do a print of the file paths everything looks good.
import distutils.core
input_source = input('Enter User Name: ')
source_drive = input('Enter source drive letter: ')
directories = ["\\My Documents", "\\Favorites", "\\Desktop"]
for directory in directories:
source = source_drive + ':\\Users\\' + input_source + directory
destination = 'C:\\Users\\' + input_source + directory
distutils.dir_util.copy_tree(source, destination)
I am getting the following error when trying to run it.
Traceback (most recent call last):
File "C:\Users\Eric\Documents\KoelCopy\KoelCopy.py", line 9, in <module>
distutils.dir_util.copy_tree(source, destination)
File "C:\Python33\lib\distutils\dir_util.py", line 124, in copy_tree
"cannot copy tree '%s': not a directory" % src)
distutils.errors.DistutilsFileError: cannot copy tree 'F:\Users\Nick\My Documents': not a directory
I assume this is happening because python cannot find the external drive. I have searched quite a bit, but I cannot find any examples of code like this to know what I am doing wrong. Do I need to tell the program how to get to this source drive? Thanks in advance for the help.

Categories

Resources