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.
Related
I want to check if a file exists before returning it. So inside my script, I did:
print("***********")
print(args.src_path)
print(os.path.isdir(args.src_path))
But the terminal returns that:
(val_env) jovyan#jupyter-me:~/use-cases/UC_Scene_understanding/Code_Woodscape/scripts$ python semantic_map_generator.py --src_path ../data/instance_annotations/ --dst_path ../data/semantic_annotations --semantic_class_mapping configs/semantic_mapping_9_classes.json --instance_class_mapping scripts/mappers/class_names.json
***********
../data/instance_annotations/
False
Traceback (most recent call last):
File "/home/jovyan/use-cases/UC_Scene_understanding/Code_Woodscape/scripts/semantic_map_generator.py", line 69, in <module>
src_path, dst_path, semantic_classes_mapping, instance_classes_mapping = parser_arguments()
File "/home/jovyan/use-cases/_Scene_understanding/Code_Woodscape/scripts/semantic_map_generator.py", line 63, in parser_arguments
raise Exception("Error: Check if the files or dirs in arguments exit!")
Exception: Error: Check if the files or dirs in arguments exit!
However, to me this ../data/instance_annotations/ is an example of dir that os.path.isdir(args.src_path) should return True.
Furthermore this directory exists:
$find
...
./scripts/semantic_map_generator.py
./scripts/polygon_generator.py
./scripts/mappers
./scripts/mappers/class_names.json
...
./scripts/box_2d_generator.py
./data
./data/semantic_annotations
./data/download.txt
Considering that this error is not related to this part and os.path.isdir() method returns if False the file or directory doesn't exist, this error is related to the later lines of this code (probably somewhere in this code you open and read a file) but
for checking existence of a file or directory I recommend you to try .exists() method instead of .isdir() method.
import os
os path.exists("blob/blob/blobfile.txt")
Reference: os.path.exists(path) [Python-doc]
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)
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.
When i run this code:
for i in range(len(ftp_folders)): #ftp_folders[] list
ftp.cwd(ftp_folders[i]) #first interaction __> ftp.folders[Templates]
print(ftp.pwd()) #entering int the folder
print('entering in : ',ftp_folders[i]) # 1. Templates
ftp.cwd(ftp_folders[i]) #ERROR WHY??
ftp.retrlines('LIST')
for loop crashes in the first interaction.
I can enter in the first folder of ftp_folders[i] (Templates) but when run the ftp.cwd(), it crashes
error:
/home/pi/Templates
entering in : Templates
Traceback (most recent call last):
File "C:\Users\peces\Documents\python\exercises\ftp\ping.pyw", line 47, in <module> ftp.cwd(ftp_folders[i])
File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 629, in cwd
return self.voidcmd(cmd)
File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 276, in voidcmd
return self.voidresp()
File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 249, in voidresp
resp = self.getresp()
File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 244, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 Templates: No such file or directory
No such file or directory?. I cant explain. sure the problem is other..
ftp.cwd(ftp_folders[i]) run as error.. And either know why.
some help?
You currently do the following:
change the remote directory to Templates.
since it is a relative path, this changes your directory relative to the directory you were in at login.
print your working directory;
you now attempt to cwd Templates again
this is a relative path, you are attempting to change to the Templates subdirectory of the Templates directory you are already in.
Note that the format of a path to CWD is implementation dependent, but commonly they support:
paths beginning with / indicate an absolute path; while
paths not beginning with / indicate a path relative to the current PWD
ok i get it.
at the end of the iteration I had had to write: ftp.cwd('../') in order to come back in the directory. Writting that i can to come back in the directory and list each folder of the "base" folder: /home/pi.
As a result, plus adding more code it saids:
for i in range(len(ftp_folders)):
print('print before change directory')
print(ftp.cwd(ftp.pwd()+'/'+ftp_folders[i]))
print(ftp.pwd())
print('entering in : ',ftp_folders[i])
#ftp.cwd('Templates')
#ftp.cwd(ftp_folders[i])
ftp.retrlines('LIST')
ftp.cwd('../')
Now it runs.!
thanks a lot #donkopotamus
When I run my code from main, it runs perfectly fine, but when I try to build main into an exe using py2exe, it gives this error:
Traceback (most recent call last):
File "main.py", line 118, in <module>
menu.menu.Menu()
File "menu\menu.pyo", line 20, in __init__
File "settingsManager.pyo", line 61, in getSetting
File "settingsManager.pyo", line 148, in __init__
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\Users\\digiholic\\git\\universalSmashSystem\\main.exe\\settings\\rules/*.*'
The line it's referring to is:
for f in os.listdir(os.path.join(os.path.dirname(__file__),'settings','rules')):
It looks like os.listdir is using unix file pathing to find every file, and Windows is having none of that. Is there a way to use listdir in a way that won't blow up everything?
When you're running within the exe you need to check if the module is frozen, the path from __file__ is generally not what you expect when you're within an exe vs the raw python script. You need to access the location with something like this:
import imp, os, sys
def main_is_frozen():
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze
def get_main_dir():
if main_is_frozen():
return os.path.dirname(sys.executable)
return os.path.dirname(sys.argv[0])
Source: http://www.py2exe.org/index.cgi/HowToDetermineIfRunningFromExe
You can also check another direction here: http://www.py2exe.org/index.cgi/WhereAmI