I know there are questions like that but I still wanted to ask this because I couldn't solve, so there is my code:
#! python3
import os
my_path = 'E:\\Movies'
for folder in os.listdir(my_path):
size = os.path.getsize(folder)
print(f'size of the {folder} is: {size} ')
And I got this error:
Traceback (most recent call last):
File "c:/Users/ataba/OneDrive/Masaüstü/Programming/python/findingfiles.py", line 7, in <module>
size = os.path.getsize(folder)
File "C:\Program Files\Python37\lib\genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'FordvFerrari1080p'
When I write print(folder) instead of getting their size it shows the folders so I don't think the program can't find them.
The problem may be that you are passing as an argument to os.path.getsize() just the folder name instead of the whole path to the folder
It may be that you have the file name as 'FordvFerrari1080p' rather than 'FordvFerrari1080p.mp4' (or whatever file type it may be)
Related
I've got some data I need to load using ytree which can be done via:
import ytree
a = ytree.load('ctrees.h5')
However, doing this gives the following error:
OSError: file does not exist: ctrees.h5.
With traceback:
Traceback (most recent call last):
File "C:\Users\Documents\untitled1.py", line 3, in <module>
a = ytree.load('ctrees.h5')
File "C:\Users\anaconda3\lib\site-packages\ytree\data_structures\arbor.py", line 1089, in load
raise IOError("file does not exist: %s." % filename)
OSError: file does not exist: ctrees.h5.
The same happens if I try a = ytree.load("ctrees.h5"). I can see that the file exists, I have changed the working directory to the correct one and my script is saved in the same directory. I also tried specifying the full path to the file with no joy.
I'm using Python 3.7 and ytree 2.3 on Windows 10
Any help appreciated.
EDIT (RESOLVED):
Fixed the issue by specifying path at top of script and reading file as:
PATH='C:\\path\\to\\file\\directory\\'
a = ytree.load(PATH + "ctrees.h5")
This is my Python code:
from plugin import Plugin
import logging
import yaml
log = logging.getLogger('discord')
def get_bot_prefix():
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
prefixValue = yaml.safe_load(prefixReader)
prefixString = prefixValue['settings']['prefix']
return prefixString
prefix = get_bot_prefix()
However I got an error with the file accessing:
Traceback (most recent call last):
File "C:/Users/85251/Documents/Discord Bots/Python/HarTex/bot.py", line 20, in <module>
from plugins.help import Help
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 30, in <module>
prefix = get_bot_prefix()
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 22, in get_bot_prefix
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
FileNotFoundError: [Errno 2] No such file or directory: 'HarTex/hartexConfig.yaml'
How can I fix it? Or am I completely wrong with the directory?
The script should work if you are calling it from the parent directory of HarTex, maybe you are running it from a different working directory?
You could also try to open the file using the full path, as this is probably easy to check.
Error is very clear.You should use absolute path instead of relative path.
For an example home/Prakash/Desktop/test12/test.yaml
Your code definitely work,Once you will change path like this.
I have just started to learn programming and I am currently trying to read an excel file from IDLE. I'm following instruction from the book "Automate the Boring Stuff". I have successfully imported openpyxl, and thereafter, as instructed tried wb = openpyxl.load_workbook('example.xlsx') where I exchanged "example" to the actual name of the workbook. However, I get this error message:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 117, in load_workbook
archive = ZipFile(filename, 'r', ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1216, in __init__
self.fp = io.open(file, filemode) FileNotFoundError: [Errno 2] No such file or directory: 'jan.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
wb = openpyxl.load_workbook('jan.xlsx')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 145, in load_workbook
raise InvalidFileException(unicode(e))
openpyxl.exceptions.InvalidFileException: [Errno 2] No such file or directory: 'jan.xlsx'
I don't understand how to solve this.
This error message simply says that python can't locate the file. When you try to open a file 'jan.xlsx', it's trying to locate it in the base directory of your code in your IDE. So say your code is in a directory called /Users/username/PycharmProjects/myCode
(I'm assuming here you are on a Mac OS as the path to your python suggests...
but jan.xlsx is in /Users/username
Since that is 2 directories up from your code directory, you can do one of two things:
Write in the absolute path to the file:
wb = openpyxl.load_workbook('/Users/username/jan.xlsx')
Use a relative path that is relative to the base project directory. Two dots in a relative path means one level up from the current directory. So if the excel file is 2 levels up, you can do:
wb = openpyxl.load_workbook('../../jan.xlsx')
I am simply trying to open a .docx file, which is stored in the server shared area.
I have tried the following:
notesPath = '//SERVER/shared_data/FolderOne/notes.docx'
os.chdir('//SERVER/shared_data')
os.startfile(notesPath)
With the os.chdir I am changing the path for command prompt, as I believe thats the issue. Becasue when I call os.getcwd() it returns me C:\\Users\\Userone\\Desktop\\. Thats what I though it was good idea to use os.chdir, convert it to //server and go from there.
But when I change the os.chdir in my code, the chdir is set to:
'\\\\SERVER\\shared_data'
with too many slashes
How can I resolve this issue?
Traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Userone\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Userone\Desktop\project\Main App.py", line 293, in noteopen
os.startfile(notesPath)
FileNotFoundError: [WinError 2] The system cannot find the file specified: '//SERVER/shared_data/FolderOne/notes.docx'
Assuming it's a UNC path, just use a raw string and use back slashes. There should be no need to change the current directory.
notes_path = r'\\SERVER\shared_data\FolderOne\notes.docx'
os.startfile(notes_path)
file_path :- find your application or other file path. put as it is.
file_path = r'C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE'
os.startfile(file_paht)
copy of the application path and pest it.
I'm starting a python script to parse a number of small text files in a folder. I need to retrieve particular information that will always be different (in this case hostname, model & serial number for a Cisco switch) and so can't use a regular expression. However I can easily find the line that contains the information. This is what I have so far:
import os
def parse_files(path):
for filename in os.listdir(path):
with open(filename,'r').read() as showfile:
for line in showfile:
if '#sh' in line:
hostname = line.split('#')[0]
if 'Model Number' in line:
model = line.split()[-1]
if 'System serial number' in line:
serial = line.split()[-1]
showfile.close()
path = raw_input("Please specify Show Files directory: ")
parse_files(path)
print hostname,model,serial
This, however, is returning:
Traceback (most recent call last):
File "inventory.py", line 17, in <module>
parse_files(path)
File "inventory.py", line 5, in parse_files
with open(filename,'r').read() as showfile:
IOError: [Errno 2] No such file or directory: 'Switch01-run.txt'
where 'Switch01-run.txt' is a file in the specified folder. I can't figure out where I'm taking a wrong turn.
The problem is that os.listdir() is returning the filenames from the directory, not the complete path to the file.
You need to do this instead:
with open(os.path.join(path,filename),'r') as showfile:
This fixes two issues - the IOerror, and the error you will get trying to read lines from a string.