filesystems

Generic way to check validity of file name in Python?

Generic way to check validity of file name in Python? Question: Creating a file can fail in Python because of various file name related reasons: the file path is too long (see e.g.: what-is-the-maximum-length-of-a-file-path-in-ubuntu ) the file name may be too long, as the file system is encrypted – see e.g. comment in answer to …

Total answers: 2

Change directory in python – os.chdir('/tmp') vs os.system("cd " + backup_location)

Change directory in python – os.chdir('/tmp') vs os.system("cd " + backup_location) Question: I’m experimenting with using Python for backups, because my Bash script became too big and complicated. I’m totally new in Python, I’m not fan of it, but it seems like Python is perfect tool for such complicated scripts. I have found something to …

Total answers: 1

How do I save a FastAPI UploadFile which is a zip file to disk as .zip?

How do I save a FastAPI UploadFile which is a zip file to disk as .zip? Question: I’m uploading zip files as UploadFile via FastAPI and want to save them to the filesystem using async aiofiles like so: async def upload(in_file: UploadFile = File(…)): filepath = /path/to/out_file.zip async with aiofiles.open(filepath, ‘wb’) as f: while buffer …

Total answers: 1

Directory.GetFiles("path/to/dir", "*.*", SearchOption.AllDirectories) but in Python

Directory.GetFiles("path/to/dir", "*.*", SearchOption.AllDirectories) but in Python Question: There’s Directory.GetFiles("path/to/dir", "*.*", SearchOption.AllDirectories) in C#,but is there same or similar function to search files in all directories? Asked By: Natan || Source Answers: You can use os.walk(‘path/to/dir’) From the documentation– Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For …

Total answers: 2

pythonic way to identify a local file or a url

pythonic way to identify a local file or a url Question: URL http://www.example.com www.example.com http://example.com https://example.com LOCAL FILES file:///example.html /home/user/example.html ./home/user/example.html .dir/data/example.html Consider above input and identify whether given input string is local regular file or a URL? What i have Tried import os from urllib.parse import urlparse def is_local(_str): if os.path.exists(path1): return True elif …

Total answers: 1

How do I get the child folder name of the path besides these methods?

How do I get the child folder name of the path besides these methods? Question: Of the given path like “level1/level2/level3/”, I’d like pass it through some operation and get the result like “level3/”. So I made two trials like these: TRIAL 1: After finding parent property within the Path object, I looked for something …

Total answers: 2

How do I iterate over files in a directory including sub directories, using Python?

How do I iterate over files in a directory including sub directories, using Python? Question: I try writing a script that counts the lines of code in a project. The problem I have is it doesn’t find all files. The script looks like this: import os root = r"C:Usersusernamedataprojectstest" allLines = [] for path, subdirs, …

Total answers: 1

How to construct an in-memory virtual file system and then write this structure to disk

How to construct an in-memory virtual file system and then write this structure to disk Question: I’m looking for a way to create a virtual file system in Python for creating directories and files, before writing these directories and files to disk. Using PyFilesystem I can construct a memory filesystem using the following: >>> import …

Total answers: 2

Get name (not full path) of subdirectories in python

Get name (not full path) of subdirectories in python Question: There are many posts on Stack Overflow that explain how to list all the subdirectories in a directory. However, all of these answers allow one to get the full path of each subdirectory, instead of just the name of the subdirectory. I have the following …

Total answers: 4