Traverse a directory over http - python

Suppose I have a url http://example.com/result, which will open a page,has some(number of directory could be one,two,three... any number of directories) directories. I want to traverse each directory and find out the new.txt file,which can be any where inside a dir or sub dir....
http://example.com/result has following dir:
security
major
minor
fails
logs
..
I need to find the new.txt inside every dir and want to read the content.
All the directories (security/major/...etc) might have sub dir also.
I need to find the new.txt inside a dir or sub directory.

If you want to do with python then you have to use urllib.
Check for the headers of each page. For directory and file there will be link tag. Go to that link tag and check for the headers. It might be possible that headers for file and directory will be different.
If its directory then recursive call the same function and check for each file in that directory.

Related

Python tarfile.extract func not extracting content of directory

I'm trying to extract a directory from tarfile using python. But some/ALL of its files inside that directory are missing after extraction. Only pathname got extracted (ie, I get folder home inside /tmp/myfolder but its empty)
Code is as follwing:
for tar in tarfiles:
mytar = tarfile.open(tar)
for file in mytar:
if file == "myfile":
mytar.extract('home', /tmp/myfolder)
Found a fix, by default extract only extracts path of variable, I can get content with
tar.extractall(members=members(tar))
Reference:
https://stackoverflow.com/a/43094365/20223973

Python Deleting All Folders but not files

I am looking to write a piece of python code that deletes all folders and their contents, but does not delete individual files.
For example here are some files and folders contained in a directory (Folder B) along with the script file that does the deleting. How do I delete folderA, folderB,folderC,etc, but leave the files? Thanks
/Folder B
file.docx
fileB.docx
fileC.docx
pythonDeleteScript.py
folderA/
folderB/
folderC/
folderD/
Use os.listdir() to get the contents of the directory, os.path.isdir(path) to see if it is a folder, and if it is, shutil.rmtree(path) to delete the folder and all its content.

Python: For each directory in the working directory go into that directory and get the name of the directory

I need to make a script that will iterate through all the directories inside a directory. It should go into each directory, get its name and save it to a variable and comes back out, and then loops.
for dir in os.walk(exDir):
path = dir
os.chdir(path)
source = #dir trimmed to anything after the last /
os.chdir("..")
loops
It needs to go into the directory to do other things not mentioned above. I've only just started Python and have been stuck on this problem for the last day or so.
For each iteration of your for loop, dir is a tuple of format (filepath, subdirectories, files). As such dir[0] will give you the filepath.
It sounds like you just want to os.chdir for each folder recursively in exDir in which case the following will work:
for dir in os.walk(exDir):
os.chdir(dir[0])
...

Create a package files python

I need to create a script to copy all files .class and .xml from multiple folders and generate a package something like tar type, those diferent path folders will be filled when the script runs, is this possible?
I'm using linux - Centos
Thanks
Python's standard library comes with multiple archiving modules, and more are available from PyPI and elsewhere.
I'm not sure how you want to fill in the paths to the things to include, but let's say you've already got that part done, and you have a list or iterator full of (appropriately relative) pathnames to files. Then, you can just do this:
with tarfile.TarFile('package.tgz', 'w:gz') as tar:
for pathname in pathnames:
tar.add(pathname)
But you don't even have to gather all the files one by one, because tarfile can do that for you. Let's say your script just takes one or more directory names as command-line arguments, and you want it to recursively add all of the files whose names end in .xml or .class anywhere in any of those directories:
def package_filter(info):
if info.isdir() or os.path.splitext(info.name)[-1] in ('.xml', '.class'):
return info
else:
return None
with tarfile.TarFile('package.tgz', 'w:gz', filter=package_filter) as tar:
for pathname in sys.argv[1:]:
tar.add(pathname)
See the examples for more. But mainly, read the docs for TarFile's constructor and open method.

i can use this 'zipme' to download source code from gae , but can not i download another file around me

i follow this article : zipme
and i download my file successful , and i want to download another file that ex: the parent file
so i change this:
dirname=os.path.dirname
folder = dirname(__file__)
to
dirname=os.path.dirname
folder = dirname(dirname(__file__))
but the error is :
firefox can't find the file
why ?
thanks
You get the error, because something fails in the script and it won't return a valid ZIP file back in the response.
The most probable reason is because your zipme.py will be in the root of your application. So if you try to get the parent folder of your root folder (returned by dirname(__file__)) it will fail because there is no parent folder (or at least not accessible by your code).
As far as I can see there would be no reason to execute the code you want to execute, because the original dirname(__file__) should already ZIP all your application's files.

Categories

Resources