File not found error altough file does not exist - python-3.x

I am trying to read through a file using the with open () function from python. I hand in the filepath via a base path and then a relative path adding on it:
filepath = base_path + path_to_specific_file
with open (filepath) as l:
do_stuff()
base_path is using the linux home symbol ( I am using Ubuntu on a VM) ~/base_path/ since I want to have the filepath adapted on every device instead of hardcoding it.
This does not work. When I execute the code, it throws a file not found error, although the path exists. I even can open it by clicking the path in the vscode terminal.
According to this thread:
File not found from Python although file exists
the problem is the ~/ instead of /home/username/. Is there a way to replace this to have it working on every device with the correct path? I cannot comment yet on this thread since I do not have enough reputation, therefore I needed to create this new question. Sorry about that.

You can use the expanduser() from pathlib for this. Example
import pathlib
filepath = pathlib.Path(base_path) / path_to_specific_file
filepath = filepath.expanduser() # expand ~
with open(filepath) as l:
do_stuff()
This should work fine.

You can join paths e.g. with:
filepath = '/'.join((basepath, path_to_specific_file))
Or do as Kris suggested: use pathlib:
>>> basepath = Path('/tmp/')
>>> path_to_specific_file = Path('test')
>>> filepath = basepath / path_to_specific_file
>>> print(filepath)
/tmp/test
EDIT:
To access $HOME (~) you can use Path.home().

Related

File Paths From Ch 10 Python Crash Course File Path Example

I'm trying to figure out how to use File Paths. This is the example that I am given but, it makes zero sense, I tried to copy the exact path that didn't work. I'm using Pycharm.
What I tested
file_path = 'D:\PycharmProjects\Standard_Library\pi_digits.txt'
with open(file_path) as file_object:
Book Example Below
file_path = '/home/ehmatthes/other_files/text_files/filename.txt'
with open(file_path) as file_object:
The author uses the Unix system and you are using the windows system and the only difference between the two examples is the file-separator.
In Python, you can declare separators either with hard-coded (For Unix: /, for windows: \)
But you can use os.path to remove the confusion of the os separator. Just place the text file in your current directory and you can use it in the example like below:
import os.path
text_file = 'pi_digits.txt'
file_path = os.path.join(os.getcwd(), text_file)
print(file_path)
Out:
/Users/PycharmProjects/StackOverFlow-pip/pi_digits.txt
Since I'm also using a Unix system my example is similar to the book example. But If you try it in your pc you will see similar to the below:
'D:\PycharmProjects\Standard_Library\pi_digits.txt'
Then you can open the text file and read it using with open(file_path) as file_object:

Does the following program access a file in a subfolder of a folder?

using
import sys
folder = sys.argv[1]
for i in folder:
for file in i:
if file == "test.txt":
print (file)
would this access a file in the folder of a subfolder? For Example 1 main folder, with 20 subfolders, and each subfolder has 35 files. I want to pass the folder in commandline and access the first subfolder and the second file in it
Neither. This doesn't look at files or folders.
sys.argv[1] is just a string. i is the characters of that string. for file in i shouldn't work because you cannot iterate a character.
Maybe you want to glob or walk a directory instead?
Here's a short example using the os.walk method.
import os
import sys
input_path = sys.argv[1]
filters = ["test.txt"]
print(f"Searching input path '{input_path}' for matches in {filters}...")
for root, dirs, files in os.walk(input_path):
for file in files:
if file in filters:
print("Found a match!")
match_path = os.path.join(root, file)
print(f"The path is: {match_path}")
If the above file was named file_finder.py, and you wanted to search the directory my_folder, you would call python file_finder.py my_folder from the command line. Note that if my_folder is not in the same directory as file_finder.py, then you have to provide the full path.
No, this won't work, because folder will be a string, so you'll be iterating through the characters of the string. You could use the os module (e.g., the os.listdir() method). I don't know what exactly are you passing to the script, but probably it would be easiest by passing an absolute path. Look at some other methods in the module used for path manipulation.

how to move files from one folder location to other using python

I am creating a automation script and my requirement is to move some files from one folder to another and get it renamed in the meanwhile
I have tried using shutil and os module but none helped me so far
src = r'C:\\Users\\XX\\Downloads\\'
dst = r'C:\\Users\\XX\\Documents\\UIPATH_DUMP\\'
regex = re.compile('MSS_')
files = os.listdir(src)
for i in files:
if regex.match(i):
src1 = src + i
dst1 = dst + i
shutil.move(src1, dst1)
The expected result is my file should get moved to the destination location. I am not able to figure out just how will I rename it? maybe os.rename() would work?
You can use os.rename() to move the file to another path as well as rename it.
For example, if the original file is:
"/Users/billy/d1/xfile.txt"
and you would like to move it to folder "d2" and name it "yfile.txt", you can use the following line of code:
os.rename('/Users/billy/d1/xfile.txt', '/Users/billy/d2/yfile.txt')

Facing a strange "phantom filesystem" issue in Python

I first noticed this problem a bit ago when I posted this thread. Essentially, I'm getting a very strange issue where Python "sees" files that don't actually exist in my file browser when working with files.
As in, if I open a file with an absolute path, modify and print the contents of it through Python, it will print exactly what it's supposed to. But when I try to open the same file through the same absolute path on my Windows file browser, the file is not updated with my modifications.
I experienced a similar issue recently as well - renaming files. Here's a simple script I wrote to replace all spaces in filenames with underscores.
rename.py:
import os
i = 0
path = "E:/sample_path/"
for filename in os.listdir(path):
src = path + filename
dst = path + filename.replace(" ", "_")
os.rename(src, dst)
i += 1
print(str(i) + " files processed.")
Upon doing some prints in Python, I can see that all the files in the directory are being renamed correctly but it just wasn't correctly updating when I actually viewed the directory. Both in the file browser and in using the dir command. Same with creating new files in Python, they exist in the eyes of Python, but they are nowhere to be found in Windows, even with hidden files being visible and all.
Now for the interesting part: This script works if I open the python editor in cmd and import it. So I know it's all correct, no syntax errors or anything - it's just a strange error occurring with Python.
For example, if I go into command prompt and type python rename.py it won't return any errors and will even output the correct results - x files processed. but it will not actually modify any "real" files.
But if I go into command prompt and type python to bring up the cmd editor, then type import rename it gives the correct output and updates all the files correctly. So for the time being this workaround helps, but it's a very strange issue and I have yet to see anyone else encounter it. It's almost like Python creates a temporary duplicate of the filesystem and is not completing it's sync back to Windows.
I've tried reinstalling Python several times, both x64 and x86 verisons and nothing has fixed it so far.
EDIT: Here's a simplified example illustrating my issue.
write.py:
f = open("newfile.txt", "w+")
f.write("hello new file")
f.close()
read.py:
f = open("newfile.txt", "r")
for l in f.readlines():
print(l)
If I run write.py, no errors are returned. But also no file named newfile.txt is visible in my current working directory. However, if I run read.py it prints hello new file. So clearly both of these are accessing the same invisible file somewhere.

Python 3.5:Not able to remove non alpha -numeric characters from file_name

i have written a python script to rename all the files present in a folder by removing all the numbers from the file name but this doesn't work .
Note :Same code works fine for python2.7
import os
def rename_files():
#(1) get file names from a folder
file_list = os.listdir(r"D:\prank")
print(file_list)
saved_path = os.getcwd()
print("Current working Directory is " + saved_path)
os.chdir(r"D:\prank")
#(2) for each file ,rename filename
for file_name in file_list:
os.rename(file_name, file_name.translate(None,"0123456789"))
rename_files()
Can anyone tell me how to make it work.Is the translate function which is not working properly
The problem is with os.rename() portion of your code.
os.rename() requires you to give it a full path to the file/folder you want to change it to, while you only gave it the file_name and not the full path.
You have to add the full path to the folders/files directory.
so it should look like this:
def rename_files():
# add the folder path
folder_path = "D:\prank\\"
file_list = os.listdir(r"D:\prank")
print(file_list)
saved_path = os.getcwd()
print("Current working Directory is " + saved_path)
os.chdir(r"D:\prank")
# Concat the folder_path with file_name to create the full path.
for file_name in file_list:
full_path = folder_path + file_name
print (full_path) # See the full path here.
os.rename(full_path, full_path.translate(None, "0123456789"))
look up the documentation for os, heres what ive found on rename:
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file.
This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to directory descriptors.
If you want cross-platform overwriting of the destination, use replace().
New in version 3.3: The src_dir_fd and dst_dir_fd arguments.
heres a link to the documentation, hope this helps, thanks
https://docs.python.org/3/library/os.html
Others have pointed out other issues with your code, but as to your use of translate, in Python 3.x, you need to pass a dictionary mapping ordinals to new values (or None). This code would work:
import string
...
file_name.translate(dict(ord(c), None for c in string.digits))
but this seems easier to understand:
import re
...
re.sub(r'\d', '', file_name)

Resources