ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I've been trying to write a short program that logs in to my NAS drive (ftp account), lists the contents of the /data/notes/ directory and downloads a file of my choice.
It works when I explicitly specify the file in the script,
eg.
You need to understand the difference between variables and values.
Code:
>>> "a string" # double quotes are the same as single quotes
'a string'
>>> 'a string'
'a string'
>>> var='value of var'
>>> var
'value of var'
>>> 'var' # this is just another string
'var'
[xtd8865@localhost python_files]$ ./ser.py
List of files
drw-rw-rw- 1 user group 0 Jun 9 00:15 .
drw-rw-rw- 1 user group 0 Jun 9 00:15 ..
-rw-rw-rw- 1 user group 230 Jun 9 00:17 secre
-r--r--r-- 1 user group 41 Jun 9 00:26 6a55w01d5
-r--r--r-- 1 user group 6714 Jun 9 00:26 _wget_
-r--r--r-- 1 user group 41 Jun 9 00:26 config_samba
-r--r--r-- 1 user group 756 Jun 9 00:26 defcom_key
-r--r--r-- 1 user group 13090 Jun 9 00:26 find_tutorial
-r--r--r-- 1 user group 17771 Jun 9 00:26 goth
-r--r--r-- 1 user group 0 Jun 9 00:26 koka
-r--r--r-- 1 user group 6493 Jun 9 00:26 nfs_howto
-r--r--r-- 1 user group 2134 Jun 9 00:26 nis
-r--r--r-- 1 user group 18622 Jun 9 00:26 sed2
-r--r--r-- 1 user group 19635 Jun 9 00:26 sed_notes
None
Choose the file to download: nis
Traceback (most recent call last):
File "./ser.py", line 27, in <module>
nas()
File "./ser.py", line 15, in nas
plik = s.retrbinary('RETR ask', open('ask', 'wb').write)
File "/usr/lib/python2.5/ftplib.py", line 390, in retrbinary
conn = self.transfercmd(cmd, rest)
File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
return self.getresp()
File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Requested action not taken.
that's what happens if I get rid of single quotes
Code:
[xtd8865@localhost python_files]$ ./ser.py
List of files
drw-rw-rw- 1 user group 0 Jun 9 00:15 .
drw-rw-rw- 1 user group 0 Jun 9 00:15 ..
-rw-rw-rw- 1 user group 230 Jun 9 00:17 secre
-r--r--r-- 1 user group 41 Jun 9 00:26 6a55w01d5
-r--r--r-- 1 user group 6714 Jun 9 00:26 _wget_
-r--r--r-- 1 user group 41 Jun 9 00:26 config_samba
-r--r--r-- 1 user group 756 Jun 9 00:26 defcom_key
-r--r--r-- 1 user group 13090 Jun 9 00:26 find_tutorial
-r--r--r-- 1 user group 17771 Jun 9 00:26 goth
-r--r--r-- 1 user group 0 Jun 9 00:26 koka
-r--r--r-- 1 user group 6493 Jun 9 00:26 nfs_howto
-r--r--r-- 1 user group 2134 Jun 9 00:26 nis
-r--r--r-- 1 user group 18622 Jun 9 00:26 sed2
-r--r--r-- 1 user group 19635 Jun 9 00:26 sed_notes
None
Choose the file to download: nis
Traceback (most recent call last):
File "./ser.py", line 27, in <module>
nas()
File "./ser.py", line 15, in nas
plik = s.retrbinary('RETR ask', open(ask, 'wb').write)
File "/usr/lib/python2.5/ftplib.py", line 390, in retrbinary
conn = self.transfercmd(cmd, rest)
File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
return self.getresp()
File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Requested action not taken.
hmm, I guess I still remember how to do it... or do I?
Code:
#! /usr/bin/env python
import ftplib
def nas():
# connect to the ftp server
s = ftplib.FTP('192.168.1.102', 'usr', 'pswd')
print "List of files"
s.cwd("/DATA/temp/")
files = s.dir()
print files
ask = raw_input("Choose the file to download: ")
file_to_download = s.retrbinary('RETR' + ask, open(ask, 'wb').write)
s.quit()
nas()
Code:
...
Choose the file to download: nis
Traceback (most recent call last):
File "./ser.py", line 27, in <module>
nas()
File "./ser.py", line 15, in nas
file_to_download = s.retrbinary('RETR' + ask, open(ask, 'wb').write)
File "/usr/lib/python2.5/ftplib.py", line 390, in retrbinary
conn = self.transfercmd(cmd, rest)
File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
return self.getresp()
File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Requested action not taken.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.