The loop for root, dir, file in os.walk(startdir) works through these steps? get root of start dir : C:dir1dir2startdir get folders in C:dir1dir2startdir and return list of folders “dirlist” get files in the first dirlist item and return the list of files “filelist” as the first item of a list of filelists. move to the second item in dirlist and
Tag: file
Print file age in seconds using Python
I need my script to download a new file, if the old one is old enough. I set the maximum age of file in seconds. So that I would get back on track with my script writing I need example code, where file age is printed out in seconds. Answer This shows how to find a file’s (or directory’s) last
Can’t download YouTube video
I’m having trouble retrieving the YouTube video automatically. Here’s the code. The problem is the last part. download = urllib.request.urlopen(download_url).read() There’s an error message (thanks Wooble): Answer The code on the original question relies on several assumptions about the content of YouTube pages and URLs (expressed in constructs such as “url.split(‘something=’)[1]”) which may not always be true. I tested it
Inexpensive ways to add seek to a filetype object
PdfFileReader reads the content from a pdf file to create an object. I am querying the pdf from a cdn via urllib.urlopen(), this provides me a file like object, which has no seek. PdfFileReader, however uses seek. What is the simple way to create a PdfFileReader object from a pdf downloaded via url. Now, what can I do to avoid
How to check whether a file is empty or not
I have a text file. How can I check whether it’s empty or not? Answer
How to read specific lines from a file (by line number)?
I’m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this? Answer If the file to read is big, and you don’t want to read the whole file in memory at once: Note that i == n-1 for the nth line.
Read from File, or STDIN
I’ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities like grep, cut etc. So, I would like it to have the following usage How can I implement the following? if a filename is
Check if object is file-like in Python
File-like objects are objects in Python that behave like a real file, e.g. have a read() and a write method(), but have a different implementation from file. It is realization of the Duck Typing concept. It is considered a good practice to allow a file-like object everywhere where a file is expected so that e.g. a StringIO or a Socket
Size of an open file object
Is there a way to find the size of a file object that is currently open? Specifically, I am working with the tarfile module to create tarfiles, but I don’t want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work. Answer Adding ChrisJY’s idea
os.walk without digging into directories below
How do I limit os.walk to only return files in the directory I provide it? Answer Use the walklevel function. It works just like os.walk, but you can pass it a level parameter that indicates how deep the recursion will go.