filenames

Extract only certain files from a folder in python as list

Extract only certain files from a folder in python as list Question: I have the following list of files in my folder: data.txt an_123.txt info.log an_234.txt filename.txt main.py an_55.txt I would like to extract only the .txt files that have prefix an as list. The output should look like the following: [an_123.txt, an_234.txt, an_55.txt] What …

Total answers: 2

OSError: [Errno 22] Invalid argument: 'The Game (John Smith)n.txt'

OSError: [Errno 22] Invalid argument: 'The Game (John Smith)n.txt' Question: Line 37: with open("{}.txt".format(cb), ‘w’, encoding="utf8") as wrt: OSError: [Errno 22] Invalid argument: ‘The Game (John Smith)n.txt’ I am trying to write files and name them according to the book title. I believe I get the error above because of the "n" in the title, …

Total answers: 2

Elegant way in python to make sure a string is suitable as a filename?

Elegant way in python to make sure a string is suitable as a filename? Question: I want to use a user-provided string as a filename for exporting, but have to make sure that the string is permissible on my system as a filename. From my side it would be OK to replace any forbidden character …

Total answers: 3

Inverse glob – reverse engineer a wildcard string from file names

Inverse glob – reverse engineer a wildcard string from file names Question: I want to generate a wildcard string from a pair of file names. Kind of an inverse-glob. Example: file1 = ‘some foo file.txt’ file2 = ‘some bar file.txt’ assert ‘some * file.txt’ == inverse_glob(file1, file2) Use difflib perhaps? Has this been solved already? …

Total answers: 2

Should Python class filenames also be camelCased?

Should Python class filenames also be camelCased? Question: I know that classes in Python are typically cased using camelCase. Is it also the normal convention to have the file that contains the class also be camelCase’d especially if the file only contains the class? For example, should class className also be stored in className.py instead …

Total answers: 5

Preferred (or most common) file extension for a Python pickle

Preferred (or most common) file extension for a Python pickle Question: At times, I’ve seen .pickle, .pck, .pcl, and .db for files that contain Python pickles, but I am unsure what is the most common or best practice. I know that the latter three extensions are also used for other things. The related question is: …

Total answers: 1

How to get folder name, in which given file resides, from pathlib.path?

How to get folder name, in which given file resides, from pathlib.path? Question: Is there something similar to os.path.dirname(path), but in pathlib? Asked By: trainset || Source Answers: It looks like there is a parents element that contains all the parent directories of a given path. E.g., if you start with: >>> import pathlib >>> …

Total answers: 3

How to get pdf filename with Python requests?

How to get pdf filename with Python requests? Question: I’m using the Python requests library to get a PDF file from the web. This works fine, but I now also want the original filename. If I go to a PDF file in Firefox and click download it already has a filename defined to save the …

Total answers: 7

Convert a filename to a file:// URL

Convert a filename to a file:// URL Question: In WeasyPrint’s public API I accept filenames (among other types) for the HTML inputs. Any filename that works with the built-in open() should work, but I need to convert it to an URL in the file:// scheme that will later be passed to urllib.urlopen(). (Everything is in …

Total answers: 4

How do you import a file in python with spaces in the name?

How do you import a file in python with spaces in the name? Question: Do I have to take out all the spaces in the file name to import it, or is there some way of telling import that there are spaces? Asked By: lilfrost || Source Answers: You should take the spaces out of …

Total answers: 4