Trying to pull files from mapped network drive Python - 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

Related

Function cannot find most recent .zip file in Downloads folder

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)
`

Python script to create m3u files inside folders

I'm trying to run this specific python script to create .m3u files inside subfolders but it's not working and give me an error, any help is welcome.
The m3u file is a simple tracklist of the subfolder content with specified extensions and named after the subfolder, like this (extensions .aaa and .bbb are just examples):
Folder 1 (contains 'File 1.aaa', 'File 2.aaa', etc)
Folder 1.m3u generated inside Folder 1 with this list
File 1.aaa
File 2.aaa
Folder 2 (contains 'File 1.bbb', 'File 2.bbb', etc)
Folder 2.m3u generated inside Folder 2 with this list
File 1.bbb
File 2.bbb
Here is the script called makem3u.py (not mine, I don't know much about python):
#!/usr/bin/python
"""This script will create an m3u file with a tracklist of each .aaa or .bbb
found in a subdirectory, then name the m3u after the subdirectory. This helps
with multiple disks for emulation.
"""
import os
import os.path
EXT = ['.aaa', '.bbb']
cwd = os.getcwd()
dirs = os.listdir(cwd)
for d in dirs:
contents = os.listdir(os.path.join(cwd, d))
disks = [
os.path.join(d, f) for f in os.listdir(os.path.join(cwd, d))
if os.path.splitext(f)[-1] in EXT
]
if disks:
with open(os.path.join(cwd, '{d}.m3u'.format(d=d)), 'wb') as m3u:
m3u.writelines(['{disk}\n'.format(disk=disk) for disk in disks])
I get this error when I try to run it:
Traceback (most recent call last):
File "makem3u.py", line 16, in <module>
contents = os.listdir(os.path.join(cwd, d))
NotADirectoryError: [WinError 267] The directory name is invalid: 'path\\to\\file\\makem3u.py'
makem3u.py is inside a folder with the subfolders mentioned
Windows 10, Python 3.8.5, python is installed properly and the PATH is in enviroment variables and I can run other scripts just fine
What I'm doing wrong and how can I fix this? Is there a non-python alternative like create a .bat file to do that? Sorry for so many questions, I'm a noob in these things. Thank you in advance!
Also, is there a way to batch zip all the files in the subfolders (the generated m3u + original files) and name each zip after that subfolder? This is an extra, but would be helpful if possible

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.

Can't resolve error while moving files from one directory to another directory with Python

I have a question that
"Create a program that read the names of files in directory 'Task2' the names are in format UmSn where m=1 to 40 and n=1 to 40 separate the files into different directories based on m like U1,U2,U3......U40."
Hints: use 'os' module for reading directories and filenames.
I tried to solve it but can't.
Here is my code.
import shutil
import os,fnmatch
os.chdir("D:/MCS 2/MCS4/SL/Task2")
for i in range(1,41):
os.mkdir("U"+str(i))
files = os.listdir()
pattern = "*.TXT"
for i in range(1,41):
for f in files:
if f.startswith("U"+str(i)) and fnmatch.fnmatch(f, pattern):
shutil.move(f,("U"+str(i)))
I tried a lot but can't resolve this error.
Traceback (most recent call last):
File "C:\Users\kaleemi\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'U10S1.TXT' -> 'U10\\U10S1.TXT'
Files start withU1 T0 U9 moves successfully but generate error while moving U10S1.TXT.
Hence the file also U10S1.TXTexist in directory.
Please help me to find where I am doing wrong in my code.
Perhaps you can try making sure you provide the absolute path instead with os.path.abspath():
from os.path import abspath
...
shutil.move(abspath(f),("U"+str(i)))

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