I was wondering if there was anyway I could modify my code to only post the basename of the file, instead of the entire file including the extension.. I'm new to python, so I don't know much, and I don't want to modify something and have it completely break.
import glob
import os
os.chdir( "C:/headers" )
txt = open( 'C:/files.txt', 'w' )
for file in glob.glob( "*.h" ):
with open( file ) as f:
contents = f.read()
if 'struct' in contents:
txt.write( "%s\n"%file )
txt.close()
Basically, what it does is search through a directory of header files, and if it has the struct string in the file, it'll print the files in a txt file. However, when I run it, the txt file opens with all the files listed, but I want it to only list the basename of the file, I don't need the .h at the end of it.
Please help, thank you!
root, ext = os.path.splitext(file)
name = os.path.basename(root)
root will contain the entire path of the given file name up to where the period would be before the extension, name will only be the name of the file without the leading path.
Perhaps this will help:
import glob
import os
import re
os.chdir( "C:/headers" )
txt = open( 'C:/files.txt', 'w' )
for file in glob.glob( "*.h" ):
with open( file ) as f:
contents = f.read() [...]
if 'struct' in contents:
txt.write( "%s\n"% re.sub('\.h$', '', file) )
txt.close()
Good luck!
Simply in one line..., returns the found files without any file extension, for the files found in the given search directory with the requested file extension...!
Found_BaseFile_Names= [(f.split('.'))[0] for f in os.listdir(SearchDir) if f.endswith('.txt')]
Related
I am using Python 3.7.
I want to check the file sizes of a directory with the file path to a text file.
This is my code:
import os
import glob
# folder path
folderpath = 'C:/Test'
# Get a list of files in my folder
list_of_files = filter( os.path.isfile,
glob.glob(folderpath + '*') )
# get list of files with the size in my folder
files_with_size = [ (file_path, os.stat(file_path).st_size)
for file_path in list_of_files ]
# Iterate over the files and write them to a file
for file_path, file_size in files_with_size:
with open('c:/Test/filesize.txt', 'w') as f:
print(file_size, ' -->', file_path)
I can print the result in the Python console, but I cannot manage to write my result to a text file.
Can anybody help me?
Regards,
Jan
I would ameloriate your code following way
for file_path, file_size in files_with_size:
with open('c:/Test/filesize.txt', 'a') as f:
print(file_size, ' -->', file_path, file=f)
changes: use a (append) mode and use f as value for named argument file of print function.
I'm having issues scanning through a root directory and modifying all .php files that contain a certain reference. Essentially, we're looking to move our entire database. I have to find all records of certain tables and rename them appropriately. Here's the code I have so far:
import os
import re
directory = 'C:/Users/me/Desktop/wsphp'
for root, dirs, files in os.walk(directory):
for filename in files:
if filename.endswith('.php'):
print(filename)
open_file = open(filename, 'r')
read_file = open_file.read()
regex = re.compile('OLD DATABASE NAME')
read_file = regex.sub('NEW DATABASE NAME', read_file)
write_file = open(filename, 'w')
write_file.write(read_file)
My code breaks when it attempts to open the file. The problem seems to be that 'filename' refers to JUST the filename without the entire directory ('index.php' rather than 'C:/Users/me/Desktop/wsphp/Subfolder/Subfolder2/index.php'). The root directory contains a few .php files as well as a bunch of subdirectories. Is there an easier way to go about this?
As you suspected, filename is just the filename. The path to the file is stored in root, so you need to do
open_file = open(os.path.join(root, filename), 'r')
is it possible to create a file with a specific extension, name and python with the sys module or any other module?
Thanks!
Using
f = open('myfile.myextension', 'w')
f.write('hello')
f.close()
You can create file with any extension you would like. If you want another software to read it, for example, if you want to create an excel file (xlsx) or pdf you might need additional packages.
This should work with the path and the file name:
import os
PATH = '/home/user/somepath'
FILE = 'somefile.someext'
os.makedirs( PATH, exist_ok=True) # create the PATH if not exists
full_name = os.path.join( PATH, FILE )
with open( full_name ) as fout :
print >>fout, 'some message'
I'm having trouble working with Python3's tempfile library in general.
I need to write a file in a temporary directory, and make sure it's there. The third party software tool I use sometimes fails so I can't just open the file, I need to verify it's there first using a 'while loop' or other method before just opening it. So I need to search the tmp_dir (using os.listdir() or equivalent).
Specific help/solution and general help would be appreciated in comments.
Thank you.
Small sample code:
import os
import tempfile
with tempfile.TemporaryDirectory() as tmp_dir:
print('tmp dir name', tmp_dir)
# write file to tmp dir
fout = open(tmp_dir + 'file.txt', 'w')
fout.write('test write')
fout.close()
print('file.txt location', tmp_dir + 'lala.fasta')
# working with the file is fine
fin = open(tmp_dir + 'file.txt', 'U')
for line in fin:
print(line)
# but I cannot find the file in the tmp dir like I normally use os.listdir()
for file in os.listdir(tmp_dir):
print('searching in directory')
print(file)
That's expected because the temporary directory name doesn't end with path separator (os.sep, slash or backslash on many systems). So the file is created at the wrong level.
tmp_dir = D:\Users\foo\AppData\Local\Temp\tmpm_x5z4tx
tmp_dir + "file.txt"
=> D:\Users\foo\AppData\Local\Temp\tmpm_x5z4txfile.txt
Instead, join both paths to get a file inside your temporary dir:
fout = open(os.path.join(tmp_dir,'file.txt'), 'w')
note that fin = open(tmp_dir + 'file.txt', 'U') finds the file, that's expected, but it finds it in the same directory where tmp_dir was created.
I have a directory of text files that all have the extension .txt. My goal is to print the contents of the text file. I wish to be able use the wildcard *.txt to specify the file name I wish to open (I'm thinking along the lines of something like F:\text\*.txt?), split the lines of the text file, then print the output.
Here is an example of what I want to do, but I want to be able to change somefile when executing my command.
f = open('F:\text\somefile.txt', 'r')
for line in f:
print line,
I had checked out the glob module earlier, but I couldn't figure out how to actually do anything to the files. Here is what I came up with, not working.
filepath = "F:\irc\as\*.txt"
txt = glob.glob(filepath)
lines = string.split(txt, '\n') #AttributeError: 'list' object has no attribute 'split'
print lines
import os
import re
path = "/home/mypath"
for filename in os.listdir(path):
if re.match("text\d+.txt", filename):
with open(os.path.join(path, filename), 'r') as f:
for line in f:
print line,
Although you ignored my perfectly fine solution, here you go:
import glob
path = "/home/mydir/*.txt"
for filename in glob.glob(path):
with open(filename, 'r') as f:
for line in f:
print line,
You can use the glob module to get a list of files for wildcards:
File Wildcards
Then you just do a for-loop over this list and you are done:
filepath = "F:\irc\as\*.txt"
txt = glob.glob(filepath)
for textfile in txt:
f = open(textfile, 'r') #Maybe you need a os.joinpath here, see Uku Loskit's answer, I don't have a python interpreter at hand
for line in f:
print line,
This code accounts for both issues in the initial question: seeks for the .txt file in the current directory and then allows the user to search for some expression with the regex
#! /usr/bin/python3
# regex search.py - opens all .txt files in a folder and searches for any line
# that matches a user-supplied regular expression
import re, os
def search(regex, txt):
searchRegex = re.compile(regex, re.I)
result = searchRegex.findall(txt)
print(result)
user_search = input('Enter the regular expression\n')
path = os.getcwd()
folder = os.listdir(path)
for file in folder:
if file.endswith('.txt'):
print(os.path.join(path, file))
txtfile = open(os.path.join(path, file), 'r+')
msg = txtfile.read()
search(user_search, msg)
Check out "glob — Unix style pathname pattern expansion"
http://docs.python.org/library/glob.html
This problem just came up for me and I was able to fix it with pure python:
Link to the python docs is found here: 10.8. fnmatch — Unix filename pattern matching
Quote: "This example will print all file names in the current directory with the extension .txt:"
import fnmatch
import os
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
print(file)