How to convert IDLE python files into a ZIP folder? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an assignment which needs me to convert 4 separate py files into a zip folder. Do you guys know how to do this?
This is the part of the instruction which tells me to compress it
Programs: Name your programs, q1.py, q2.py, q3.py, q4.py and submit all as a zip file(named A1_my_upi.zip) to the assignment drop box before the deadline (No late submission
accepted).
I have read on the internet that I have to import zipfile. Can some one please clairfy?

Let's say that your files are in /users/matthew/documents/school/assignments/a1. Here are a couple of ways in which you can do this:
Python:
import os
from zipfile import Zipfile
dirpath = '/users/matthew/documents/school/assignments/a1'
with open("A1_my_upi.zip", 'w') as outfile:
for fname in os.listdir(dirpath):
if not fname.endswith('.py'):
continue
outfile.write(os.path.join(dirpath, fname))
Now, you have a A1_my_upi.zip that you can submit
If you're on a linux/mac computer, you can do this:
$ cd /users/matthew/documents/school/assignments/a1
$ cd ..
$ zip A1_my_upi.zip a1/*.py
Now, you have a A1_my_upi.zip that you can submit. It exists at /users/matthew/documents/school/assignments/A1_my_upi.zip

Related

Delete all files in a directory with a similar name? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
so I am currently somewhat new in python, usually, I do programming in Node.js. I wanted to know if there is a way to delete all files that has a specific name in them?
For example, let's say I have the following files in a directory:
PDforeg.txt
PDahvn.txt
AHgme.txt
Ronra.txt
I want to be able to delete all files that includes the word "PD" in them. How do I do this?
import glob
import os
files = glob.glob("PATH_to_directory/*.txt")
for file in files:
if "PD" in file:
os.remove(file)
import subprocess
subprocess.run('rm *PD*.txt', shell=True)
Or you could import os and do os.remove() stuff.
For multi platform you can use the following script:
import glob
import os
folder = '/home/user/Documents/'
text_to_look_for = 'PD'
for f in glob.glob(folder + '*' + text_to_look_for +'*.txt'):
os.remove(f)
import pathlib
path = pathlib.Path('D:/dev/stackoverflow') # This is your folder
[p.unlink() for p in path.glob('*PD*')]
# Another way of writing the above line
for p in path.glob('*PD*'):
p.unlink()
*PD* will be all files that have PD in them somewhere, not just at the start. If you want to remove files with PD only at the start, use PD*.
In bash, just,
[user ~]$rm *.txt
This command will delete all the files with the same .txt extension in the current directory.

how to copy many files inside of a directory to many other directories using shell or python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i have many files inside a directory1(libX11.so.6, tm.txt, ff.txt..and so on), i need to copy these files of directory1 to many other directories named as directory2,3.......100000. I tried find command but its not working.My sample online code is given below.can anybody suggest some better solutions using shell or python...Thanks in advance.
find . -type f -name "libX11.so.6, tm.txt, ff.txt" -exec cp -rv {} /home/geo/data/directory{2...100000} \;
while doing this it shows errors
The following code will copy all your original_dir files to the destination_dir.
I believe you can do some customized changes to get what you want.
import os
from shutil import copyfile
original_dir = "/path/to/the/original/dir"
original_files = os.listdir(original_dir)
destination_dir = "/path/to/your/destination/dir"
for each in original_files:
current_file = os.path.join(original_dir, each)
# check if it is a file
if not os.path.isdir(current_file):
# print(each + " is being processing")
copyfile(current_file, destination_folder + each)
You could use the python library shutil to achieve this, code to make this work is below:
import shutil
shutil.copy(src, dst)

How to get the current working directory with os library and write a .txt file on it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to find the current working directory (cwd) with os library and write .txt file on it.
Something like this:
import os
data=["somedatahere"]
#get_the_current_directory
#if this_is_the_current_directory:
new_file=open("a_data.txt", "a")
new_file.write(data)
new_file.close()
It can be done using the os library, but the new pathlib is more convenient if you are using Python 3.4 or later:
import pathlib
data_filename = pathlib.Path(__file__).with_name('a_data.txt')
with open(data_filename, 'a') as file_handle:
file_handle.write('Hello, world\n')
Basically, the with_name function said, "same dir with the script, but with this name".
import os
You're halfway there!
The rest of it is:
print(os.getcwd())
Of course, you don't need to know that value,
as a_data.txt or ./a_data.txt suffices.
As a side note, you'd be better off closing with a with handler:
with open('a_data.txt', 'a') as new_file:
new_file.write(data)
Habitually using a Resource Manager means
never having to say, "sorry, forgot to close it!"

How can i cat all txt files to terminal using python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to cat txt files from a folder and cat results should be shown in terminal (Obviously). I have tried using listdir() it but it doesn't work. Required some help!
a simple implementation uses glob to generate absolute paths to files with .txt extension, in a loop which reads the file and prints it on standard output:
import glob,sys
for filepath in sorted(glob.glob("path/to/directory/*.txt")):
with open(filepath) as f:
sys.stdout.write(f.read())
using fileinput allows to read all the files, and line by line, probably less memory intensive and shorter:
import glob,sys,fileinput
for f in fileinput.input(sorted(glob.glob("path/to/directory/*.txt"))):
sys.stdout.write(f)
note that sorted is better to ensure that the files are processed in deterministic order (some filesystems don't respect that order)
sys.stdout.write(f) still writes line by line, but as suggested by comments you could improve performance and not use so much memory by using shutil.copyfileobj(f,sys.stdout)
Just use the command in terminal "cat *"
Or if you want to do it in python:
import os
allTextFileContents = os.popen("cat *").read();
print(allTextFileContents);
You can also do other things with the contents since its stored as a variable!

Running Python script over multiple files in a folder [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use python in opensuse, my problem is i need to execute large data in my folder.
for example
python myprogram.py 20140101.txt
i still need to run for a lot data with like that naming (20140101.txt) ex 20140204 etc..
my question is how to make my program running automatically for all data together.
use bash like this:
for file in /dir/*.txt
do
python myprogram.py $file
done
For a pure python solution, have a look at fileinput.
It is part of the python standard library and let's you loop over files given via standard input or via a list of files, e.g.:
import fileinput
for line in fileinput.input():
process(line)
So you could do:
./python myprogram 2014*.txt
The glob module is useful for processing multiple files with different names.
https://docs.python.org/2/library/glob.html
A python solution would be to use "glob". It helps you creating lists of files' name based on a certain pattern. You can then loop through those filenames to execute your commands on. See example below.
import glob
txt_files = glob.glob("201401*.txt")
for txt in txt_files:
print txt
my_txt_file = open(txt, "r")
For further reference:
https://docs.python.org/3/library/glob.htm

Categories

Resources