|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Another FTP question
I have a problem when retrieving text file from FTP server, the formatting will lost(the line part) eg:
Code:
--- The original file -- 1st line 2nd Line -------- the retrived file ----- 1st line2nd Line but transfering file to the server is fine. |
|
#2
|
|||
|
|||
|
Try retrieving in binary mode. The only difference between text mode and binary is that in text mode it will try to change the line endings to match the convention of your system. In your case it looks like it is getting it wrong. If you use binary mode then it will do no translation and you should get the file back exactly as it is on the server.
You need to make sure that the file object that you create to save the file is opened in the same mode, or that may change the line endings as well. Dave - The Developers' Coach |
|
#3
|
|||
|
|||
|
Quote:
I didn't really get that part.Can you explain it further?? |
|
#4
|
|||
|
|||
|
Ok.. I've tried using binary mode anbd it works thanks.Another question, how can I divide a path to a file so each patn/or directory is saved as a variable, for example:
Code:
path = tem/dir1/xml.txt so I can get it this way level1 = temp level 2 = dir1 file = xml.txt Thnak you. |
|
#5
|
|||
|
|||
|
Quote:
If you know how many levels there are then you can do it like this: Code:
>>> import os
>>> myPath = 'temp/dir1/xml.txt'
>>> myPath, file = os.path.split(myPath)
>>> myPath, level2 = os.path.split(myPath)
>>> myPath, level1 = os.path.split(myPath)
>>> myPath, level1, level2, file
('', 'temp', 'dir1', 'xml.txt')
If you don't know how many levels then you will need to loop round and append the result to a list. Alternatively if you know what the separator is, you can use string.split: Code:
>>> myPath = 'temp/dir1/xml.txt'
>>> level1, level2, file = myPath.split('/')
However this is platform dependant, since the separator is different on different OSs. You can use os.sep to get the separator character being used, but Windows allows both '/' and '\' as separators, so you can end up with a mixture. A call to os.path.normpath() will change them to '\'. Code:
>>> path = r'temp\dir1/xml.txt'
>>> level1, level2, file = os.path.normpath(path).split(os.sep)
>>> level1, level2, file
('temp', 'dir1', 'xml.txt')
Dave - The Developers' Coach |
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > Another FTP question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|