ftp.cwd() I dont understand the error - python

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

Related

"directory/" is not recognized as such by os.path.isdir()

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]

os.startfile() not opening .docx file

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.

RarFile / [WinError 5]: Access Denied

I'm trying to write a script that will automatically extract the files from a rar or zip folder and put them somewhere, so as to make file organization faster. Included are the relevant sections of code:
import shutil
import os
import eyed3
import glob
import zipfile
import rarfile
import unrar
import patoolib
## create zipfile object of the downloaded album and get a tracklist
rarfile.UNRAR_TOOL=r'C:\Users\John\AppData\Local\Programs\Python\Python36-32'
downloads = glob.glob("C:\\Users\\John\\Downloads\\*")
music_zip = max(downloads, key=os.path.getctime)
if os.path.splitext(music_zip)[-1] == '.zip':
music_folder = zipfile.ZipFile(music_zip)
elif os.path.splitext(music_zip)[-1] == '.rar':
music_folder = rarfile.RarFile(music_zip)
print(music_zip)
print(music_folder)
temporary_album_folder = 'C:\\Users\\John\\Downloads\\temporary_album_folder'
if not os.path.exists(temporary_album_folder):
os.makedirs(temporary_album_folder)
# patoolib.extract_archive(music_zip, outdir=temporary_album_folder)
# temp_list = os.listdir(temporary_album_folder)
# tag = eyeD3.load(temp_list[0])
# artist = tag.getArtist()
# album = tag.getAlbum()
# print(os.getcwd())
os.chdir(temporary_album_folder)
music_folder.extractall()
music_folder.close()
print(temporary_album_folder)
When I run this, I expect it to successfully extract the contents of the RAR into a temporary folder in \Downloads. Instead, the error message that I get when I try to run this in the console is:
C:\Users\John\Documents\PythonScripts>music_organizer.py
C:\Users\John\Downloads\d1ctus t3 n3c4r3(5).rar
<rarfile.RarFile object at 0x02C16350>
Traceback (most recent call last):
File "C:\Users\John\Documents\PythonScripts\music_organizer.py", line 40, in <
module>
music_folder.extractall()
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 820, in extractall
self._extract(fnlist, path, pwd)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 885, in _extract
p = custom_popen(cmd)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 2813, in custom_popen
creationflags=creationflags)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.p
y", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.p
y", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
I know a lot of other people have asked similar questions about WinError 5 and Python, so to address possible common suggestions in advance: I am running the terminal in admin mode, have turned off UAC, have unblocked the folder in question, and have opened full permissions to the folder and sub-folders in question. Does anyone know why this is happening and possible get arounds? Any help much appreciated.
Refer to: Eryksun's comment
It's not a security permission issue. UNRAR_TOOL should be the executable name (optionally the full path) of an unrar program. subprocess.Popen is failing because you're trying to execute the "Python36-32" directory. – eryksun yesterday
The Windows API has some rather useless error code mappings. Internally in the NT API the error in this case is STATUS_FILE_IS_A_DIRECTORY (0xC00000BA), which could not be more obvious, but it gets mapped to ERROR_ACCESS_DENIED (0x0005) by Windows, which misleads you into thinking it's a problem with file or object permissions. – eryksun yesterday

I am having issues with using pynsist

Hi I've installed pynsist in order to make my python files into executables but I am having some issues. The project consists of two files that I've written. The main program to be run is Filereader.py and a supplied file called spuriousReq.py which Filereader.py uses a function from. Currently my installer.cfg file looks like this
[Application]
name=WFilereader
version=1.0
entry_point=Filereader
console=true
[Python]
version=3.4.0
[Include]
packages = matplotlib
statistics
bisect
files = spuriousReq.py
I've moved the installer.cfg file and both python files to the C:\Python34\Scripts folder in order to access them from the cmd (Yes I am new at this..). But I get the following error which I dont know how to interpret or solve..
C:\Python34\Scripts>"C:\Python34\python.exe" "C:\Python34\Scripts\\pynsist" inst
aller.cfg
Traceback (most recent call last):
File "C:\Python34\Scripts\\pynsist", line 3, in <module>
main()
File "C:\Python34\lib\site-packages\nsist\__init__.py", line 393, in main
shortcuts = configreader.read_shortcuts_config(cfg)
File "C:\Python34\lib\site-packages\nsist\configreader.py", line 172, in read_
shortcuts_config
appcfg = cfg['Application']
File "C:\Python34\lib\configparser.py", line 937, in __getitem__
raise KeyError(key)
KeyError: 'Application'
As mentioned in the documentation [http://pynsist.readthedocs.io/en/latest/] you need to specify the function from your 'Filereader.py' file which will be starting the execution of your script. For example if you have a 'main' function that will be the entry point or starting point of your script then you need to specify that in your 'installer.cfg' file like below:-
[Application]
name=WFilereader
version=1.0
entry_point=Filereader:main <------ Here mention your entry point function.
console=true
[Python]
version=3.4.0
[Include]
packages = matplotlib
statistics
bisect
files = spuriousReq.py

Why does nltk no longer load the model file?

Setup : Python 3.4 on Anaconda , Windows 10 IPEC 64 bit VM NLTK
version 3.0.2, stanford-parser-3.5.2
I successfully implemented a function yesterday and got it working. Here is the code :
def parnow(temp_in):
import os
from nltk.parse import stanford
java_path = "/Program Files (x86)/Java/jdk1.8.0_05/bin/java.exe"
os.environ['STANFORD_PARSER'] = '/stanford'
os.environ['STANFORD_MODELS'] = '/stanford'
os.environ['JAVAHOME'] = java_path
parser = stanford.StanfordParser(model_path="/stanford/parser/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
sentences = parser.raw_parse_sents((temp_in))
for myListiterator in sentences:
for t in myListiterator:
return(t)
This was working fine yesterday and I made a couple of test runs. This morning, when I ran the same function, with for example parnow("This is a test.") I get an error message saying "NLTK was unable to find the gs file!" .
This is what I've tried :
Checked the path to the file for spelling errors or alike.
Reloaded Module, Reloaded Kernel, Restarted Spyder
Re-copied the path and pasted it again.
Replaced the file with a backup copy.
Checked code history but saw no changes made.
Placed a character in the path. When I do this, I get the "raise OSError". When I undo this change, I get the missing file error again, which shows me that the NLTK somehow sees the file afterall.
Used C:\\stanford\\parser\\edu\\stanford\\nlp\\models\\lexparser\\englishPCFG.ser.gz as path.
Went step by step without function and confirmed each line to make sure no errors where present before setting parser.
Copied file to another folder, changed path.
Changed file name.
Checked permissions of folder / file.
Checked if file was in use/locked for some reason.
[EDIT] Error message :
parnow("This is my last resort")
Out[3]: Tree('ROOT', [Tree('NP', [Tree('NNP', ['T'])])])Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 339, in __call__
return method()
File "C:\Anaconda3\lib\nltk\tree.py", line 726, in _repr_png_
subprocess.call([find_binary('gs', binary_names=['gswin32c.exe', 'gswin64c.exe'], env_vars=['PATH'], verbose=False)] +
File "C:\Anaconda3\lib\nltk\internals.py", line 546, in find_binary
binary_names, url, verbose))
File "C:\Anaconda3\lib\nltk\internals.py", line 540, in find_binary_iter
url, verbose):
File "C:\Anaconda3\lib\nltk\internals.py", line 519, in find_file_iter
raise LookupError('\n\n%s\n%s\n%s' % (div, msg, div))
LookupError:
===========================================================================
NLTK was unable to find the gs file!
Use software specific configuration paramaters or set the PATH environment variable.
===========================================================================

Categories

Resources