I have code shown below
for root, dirs, files in ftp_host.walk(ftp_host.curdir, topdown=False):
if ftp_host.path.isdir(root):
for folder in ftp_host.path.normpath(root).split('/'):
target_dir = os.path.join(target_dir, folder)
if not os.path.exists(target_dir):
os.mkdir(target_dir)
for name in files:
target_file = os.path.join(target_dir, name)
# print name - I am able to see all files here including *.JPG
if ftp_host.path.isfile(name):
# print name - there no any *.JPG
ftp_host.download(name, target_file)
target_dir = curdir
When I am trying to download recursively all files from target FTP, but I can't any JPG's.
Please advice where I am wrong
My bad... I dind't correct move to specific directory. ftputils work very well!
Related
search the same file in multiple folders
I have tried with os.walk(path) but I am not getting the nested folders traversing
for current_root, folders, file_names in os.walk(self.path, topdown=True):
for i in folders:
print i
for filename in file_names:
count+= 1
file_path = os.path.join(current_root + '\\' + filename)
#print file_path
self.location_dictionary[file_path] = filename
in my code, it will print all folders but it will not enter to the nested folders recursively
ex: I have subdir,subdir1,subdir2 and in subdir I have another dir called abc
in subdir and abc both contain same file name I want to read that file
os.walk does not work that way.
for each current_root it traverses, it provides the list of directories and files directly under it.
You're nesting the loops, which does ... well I don't know...
Here you don't need the folder (so just mute the argument). current_root already contains that info for your files:
for current_root, _, file_names in os.walk(self.path, topdown=True):
for filename in file_names:
count+= 1
file_path = os.path.join(current_root,filename)
#print file_path
self.location_dictionary[file_path] = filename
aside: creating a dictionary with full file as key and filename as value looks, well, not what you want (the same information could be stored in a set or list and os.path.basename could be used to compute the filename. Maybe it's reverse (filename => full path), provided that there are no duplicate filenames.
I try to follow an example in an online course to create a function to change the files name by omitting the numbers from the files name. But I got this error:
os.rename(file_name, file_name.translate(None,"0123456789"))
WindowsError: [Error 2] The system cannot find the file specified
But the code runs fine on the video that I watched
import os
def rename_files():
# (1) get file naems from a folder
file_list = os.listdir(r"C:\prank")
#print (file_list)
saved_path = os.getcwd()
print (" Currnet Wroking Directory is " +saved_path)
os.chdir(r"C:\prank")
# (2) for each file, rename filename
for file_name in file_list:
os.rename(file_name, file_name.translate(None,"0123456789"))
os.chdir (saved_path)
rename_files()
Try this one out, there is no need to change the working directory, its not a good practice. Also if you are renaming files in C: you may have to execute the code as administrator.
import os
def rename_files():
# directoy with the files
directory=r"C:\prank"
# (1) get file names from a folder
file_list = os.listdir(directory) # this is the folder with the files
# (2) for each file, rename filename
for file_name in file_list:
new_name=''.join([s for s in file_name if s not in ['0','1','2','3','4','5','6','7','8','9']]) # remove numbers
os.rename(os.path.join(directory,file_name),os.path.join(directory,new_name))
rename_files()
I have my python code that runs a C++ code, which takes files in another folder as input.
I have my codes in folder A, and the input files are in folder B, and I have been trying this:
path = 'C:/pathToInputFiles'
dirs = os.listdir(path)
for path in dirs:
proc = subprocess.Popen([fullPathtoCppCode, inputFiles])
However, I keep receiving WindowsError: [Error 2] The system cannot find the file specified
The only way it works is when I put the C++ executable file in the same folder of the input files, which I am avoiding to do.
How can I make python reads the file path properly?
Try using os.path.join after your for statement.
path = os.path.join(directory, filename)
for example
def test(directory):
for filename in os.listdir(directory):
filename = os.path.join(directory, filename)
proc = subprocess.Popen([fullPathtoCppcode, inputFiles])
import os
myDir = "C:\\temp\\a"
for root, dirs, files in os.walk(myDir):
for file in files:
# fname = os.path.join(root, file) # this works fine, yeah!
fname = os.path.join(myDir, file)
print ("%r" % (fname))
src = os.path.isfile(fname)
if src == False:
print ("%r :Fail" % (fname))
f = open(fname,"r")
f.close()
I expected the two versions of fname to be the same, but I've found out the hard way that the above code doesn't work. I just want to know why, that's all.
The problem is that os.walk(myDir) walks all the subdirectories, recursively! When walk descends into a subdirectory, root will be that directory, while myDir is still the root directory the search started in.
Let's say you have a file C:\temp\a\b\c\foo.txt. When os.walk descends into c, myDir is still C:\temp\a and root is C:\temp\a\b\c. Then os.path.join(root, file) will yield C:\temp\a\b\c\foo.txt, while os.path.join(myDir, file) gives C:\temp\a\foo.txt.
You might want to rename your myDir variable to root, and root to current, respectively, so it's less confusing.
My problem is to read '.csv' files in catalogs and do some calculations on them.
I have calculations working but my for loop seem not to work as I want to.
d = 'F:\MArcin\Experiments\csvCollection\'
for dirname, dirs, files in os.walk(d):
for i in files:
if i.endswith('.csv'):
data1 = pd.read_csv(i, sep=",")
data = data1['x'][:, np.newaxis]
target = data1['y']
The error Iam getting is:
IOError: File 1.csv does not exist
files is list of all '.csv' files inside dirname
i is str of size 1 and contains 1.csv (that is first of the files in catalog)
Any ideas why this is not working?
Thanks for any help.
Because 1.csv is somewhere on the filesystem and when you call read_csv() it opens file relative to current directory.
Just open it using absolute path:
data1 = pd.read_csv(os.path.join(dirname, i), sep=",")
dirname in os.walk represents actual directory where file 1.csv is located.