How do I go about reading from or writing to a file that isn't in the same folder as the file itself.
I'm trying to write a function that allows user input to find the file by using the path itself. I have to set it up where the user inputs the file name, then the path
I have to then concatenate them together to form the finished path to locate the file.
Use os.path.join() to construct file paths from distinct parts:
import os.path
dirname = '/foo/bar/baz'
filename = 'ham_n_spam'
path = os.path.join(dirname, filename)
Related
I am currently accessing a script that opens a file in the directory it's located in. I am accessing this file from both the main.py file located in the same directory, as well as a testfile which is located in a "Test" subdirectory. Trying to use a file from the Test subdirectory to call the function that opens the file causes the script to try and open it from the Test directory instead of the super directory, since I am opening the file simply by calling it as following:
with open(filename,"w") as f:
Is there a way to define the location of the file in a way that makes it possible for the script opening it to be called from anywhere?
Use __file__ to get the path to the current script file, then find the file relative to that:
# In main.py: find the file in the same directory as this script
import os.path
open(os.path.join(os.path.dirname(__file__), 'file.txt'))
# In Test/test.py: find the file one directory up from this script
import os.path
open(os.path.join(os.path.dirname(__file__), '..', 'file.txt'))
just give the absolute file path instead of giving a relative one
for eg
abs_path = '/home/user/project/file'
with open(abs_path, 'r') as f:
f.write(data)
Try specifying the path:
import os
path = 'Your path'
path = os.path.abspath(path)
with open(path, 'w') as f:
f.write(data)
From what I understood your file is in a directory parent_directory/file_name.txt
and in another folder parent_directory/sub_directory/file_name.txt. All you have to do is paste the below code in both parent and sub directories.
import os
file_name = 'your_file_name'
# if the file is in current directory set the path to file_name
if file_name in os.listdir(os.getcwd()):
path = file_name
# if the path is not in current directory go back to parent directory set the path to parent directory
else:
path = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
print('from',os.getcwd())
with open(path, 'r') as filename:
print(filename.read())
i have two folder and each folder has six file with different extension (.txt, .jpg, .tiff etc) and i want to rename each file name with same name (don't want to change file extension) using Python.
could please help me out. I am new to programming and am starting to learn Python
Thanks in advance.
List all files in the directory using os.listdir, and then use os.rename
to rename all files, also use os.path.splitext to extract extension and file name
import os
folder_name = '<folder_where_we_have_files>'
name = '<name_for_file>'
for file in os.listdir(folder_name):
#Get the full file path
file_path = os.path.join(folder_name, file)
#Get the file name and it's extension
file_name, extension = os.path.splitext(file_path)
#We don't want to rename file which is already renamed
if name not in file_name:
#Create the full path of the new file
new_file = os.path.join(folder_name, name + extension)
#Rename the file to new file
os.rename(file_path, new_file)
Try this:-
for filename in os.listdir(folder_name):
src=foldername+filename
dst=new_name+filename.split(".")[1]
dst=folder_name+dst
os.rename(src,dst)
I'm trying to run two operations:
Starting from a .txt file containing some IDs (which lead to a filename), checking if that file is within a folder;
If step 1) is true, copying the file from that folder to a specified folder.
The .txt file stores codes like these:
111081
112054
112051
112064
This is what I have tried:
from glob import glob
from shutil import copyfile
import os
input = 'C:/Users/xxxx/ids.txt'
input_folder = 'C:/Users/xxxx/input/'
dest_folder = 'C:/Users/xxxx/output/'
with open(input) as f:
for line in f:
string = "fixed_prefix_" + str(line.strip()) + '.asc'
if os.path.isfile(string):
copyfile(string, dest_folder)
The string variable generates this (for example):
print string
fixed_prefix_111081.asc
Then, I'm sure there is something else wrong with the searching and the copying of the file to the destination folder. The main problem is that I don't know how to search for the fixed_prefix_111081.asc file in the input_folder.
copyfile expects a filename as destination. Passing an existing directory is the case where it doesn't work. Using copy handles both cases (target directory or target file)
the input file seems to be passed without path. You'd have to generate the full filename if you're not in input_folder or os.path.isfile will always be False
My fix proposal:
with open(input) as f:
for line in f:
string = "fixed_prefix_{}.asc".format(line.strip())
fp_string = os.path.join(input_folder,string)
if os.path.isfile(fp_string):
copy(fp_string, dest_folder)
I have a Python script which opens a specific text file located in a specific directory (working directory) and perform some actions.
(Assume that if there is a text file in the directory then it will always be no more than one such .txt file)
with open('TextFileName.txt', 'r') as f:
for line in f:
# perform some string manipulation and calculations
# write some results to a different text file
with open('results.txt', 'a') as r:
r.write(someResults)
My question is how I can have the script locate the text (.txt) file in the directory and open it without explicitly providing its name (i.e. without giving the 'TextFileName.txt'). So, no arguments for which text file to open would be required for this script to run.
Is there a way to achieve this in Python?
You could use os.listdir to get the files in the current directory, and filter them by their extension:
import os
txt_files = [f for f in os.listdir('.') if f.endswith('.txt')]
if len(txt_files) != 1:
raise ValueError('should be only one txt file in the current directory')
filename = txt_files[0]
You Can Also Use glob Which is easier than os
import glob
text_file = glob.glob('*.txt')
# wild card to catch all the files ending with txt and return as list of files
if len(text_file) != 1:
raise ValueError('should be only one txt file in the current directory')
filename = text_file[0]
glob searches the current directory set by os.curdir
You can change to the working directory by setting
os.chdir(r'cur_working_directory')
Since Python version 3.4, it is possible to use the great pathlib library. It offers a glob method which makes it easy to filter according to extensions:
from pathlib import Path
path = Path(".") # current directory
extension = ".txt"
file_with_extension = next(path.glob(f"*{extension}")) # returns the file with extension or None
if file_with_extension:
with open(file_with_extension):
...
I have a small text (XML) file that I want a Python function to load. The location of the text file is always in a fixed relative position to the Python function code.
For example, on my local computer, the files text.xml and mycode.py could reside in:
/a/b/text.xml
/a/c/mycode.py
Later at run time, the files could reside in:
/mnt/x/b/text.xml
/mnt/x/c/mycode.py
How do I ensure I can load in the file? Do I need the absolute path? I see that I can use os.path.isfile, but that presumes I have a path.
you can do a call as follows:
import os
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
This will get you the directory of the python file you're calling from mycode.py
then accessing the xml files is as simple as:
xml_file = "{}/../text.xml".format(BASE_DIR)
fin = open(xml_file, 'r+')
If the parent directory of the two directories are always the same this should work:
import os
path_to_script = os.path.realpath(__file__)
parent_directory = os.path.dirname(path_to_script)
for root, dirs, files in os.walk(parent_directory):
for file in files:
if file == 'text.xml':
path_to_xml = os.path.join(root, file)
You can use the special variable __file__ which gives you the current file name (see http://docs.python.org/2/reference/datamodel.html).
So in your first example, you can reference text.xml this way in mycode.py:
xml_path = os.path.join(__file__, '..', '..', 'text.xml')