@lakshayarora7/

SeveralGregariousBassethound

Python

No description

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.981081696975955","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"IyBSZW1vdmluZyBjb21tZW50cyBmcm9tIGEgZmlsZQ0KIyBXcml0ZSBhIHByb2dyYW0gdG8gcmVtb3ZlIGFsbCB0aGUgY29tbWVudHMgZnJvbSBhIGZpbGUuIENyZWF0ZSBhIG5ldyBmaWxlIGFuZCBsZWF2ZSB0aGUgb3JpZ2luYWwgdW50YW1wZXJlZC4gDQojIFN1Y2ggYW4gYWN0aXZpdHkgaXMgdXN1YWxseSBwZXJmb3JtZWQgd2hpbGUgY3JlYXRpbmcgbWluaWZpZWQgdmVyc2lvbiBvZiBhIHNjcmlwdCwgdG8gcmVkdWNlIGZpbGUgc2l6ZS4NCg0KIyBhc2sgZm9yIGZpbGUgbmFtZXMNCmlucHV0RmlsZU5hbWUgPSBpbnB1dCgnUGxlYXNlIGVudGVyIHRoZSBuYW1lIG9mIHRoZSBmaWxlIHlvdSB3aXNoIHRvIHJlbW92ZSBjb21tZW50cyBmcm9tOiAnKQ0Kb3V0cHV0RmlsZU5hbWUgPSBpbnB1dCgnUGxlYXNlIGVudGVyIHRoZSBuYW1lIG9mIHRoZSBuZXcgZmlsZTogJykNCg0KIyBvcGVuIGJvdGggZmlsZXMNCmlucHV0RmlsZUhhbmRsZXIgPSBvcGVuKGlucHV0RmlsZU5hbWUsICdyJykNCm91dHB1dEZpbGVIYW5kbGVyID0gb3BlbihvdXRwdXRGaWxlTmFtZSwgJ3cnKQ0KDQojIHJlYWQgdGhlIGZpbGUgbGluZSBieSBsaW5lOyB3cml0ZSB0byBuZXcgZmlsZSBhcyBpdCBpcyBpZiBubyBjb21tZW50IGluIGxpbmU7IG1vZGlmeSBhbmQgdGhlbiB3cml0ZSB0byBuZXcgZmlsZSBpZiBjb21tZW50IHByZXNlbnQNCiMgZmluZ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XN0ID0gWzEsIDIsIDMsIDQsIDUsIDZdCQkJCSMgYSBsaXN0IHZhcmlhYmxlXG4nKQ0KIyNmaWxlSGFuZGxlcjIud3JpdGUoJ215U2V0ID0gc2V0KG15TGlzdCkJCQkjIGNyZWF0aW5nIGEgc2V0IGZyb20gYSBsaXN0XG4nKQ0KIyNmaWxlSGFuZGxlcjIud3JpdGUoJ215VHVwbGUgPSAyLCAzCQkJCSMgY3JlYXRpbmcgYSB0dXBsZSB3aXRob3V0IHBhcmVudGhlc2VzXG4nKQ0KIyNmaWxlSGFuZGxlcjIud3JpdGUoJ215RGljdCA9IHsxOiBcJ29uZVwnLCAyOiBcJ3R3b1wnfQkJIyBjcmVhdGluZyBhIGRpY3Rpb25hcnkgdmFyaWFibGUnKQ0KIyNmaWxlSGFuZGxlcjIuc2VlaygwKQ0KIyNwcmludChmaWxlSGFuZGxlcjIucmVhZCgpKQ0K"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Removing comments from a file
# Write a program to remove all the comments from a file. Create a new file and leave the original untampered. 
# Such an activity is usually performed while creating minified version of a script, to reduce file size.

# ask for file names
inputFileName = input('Please enter the name of the file you wish to remove comments from: ')
outputFileName = input('Please enter the name of the new file: ')

# open both files
inputFileHandler = open(inputFileName, 'r')
outputFileHandler = open(outputFileName, 'w')

# read the file line by line; write to new file as it is if no comment in line; modify and then write to new file if comment present
# find() returns the position of a character/substring in a string; returns -1 if not found
# line[0: positionOfHash] slices the line till the character just behind the # symbol. In the process, the new line character after the comment is also deleted, so we manually add it here
for line in inputFileHandler.readlines():
    positionOfHash = line.find('#')

    if positionOfHash != -1:
        line = line[0 : positionOfHash]
        line = line + "\n"

    outputFileHandler.write(line)

inputFileHandler.close()
outputFileHandler.close()

# printing a success message
print(outputFileName,"has been created without comments.")


######## END OF PROGRAM ########






# I should admit that it is not the best solution to remove bits and pieces from a file. For starters, it does not have any handling for the event in which the specified does not exist, I am sure you will be able to do that once you go through Exception Handling. Another case that the program does not handle is that the pound/hash symbol may be a part of a string such as 'Et#han', which will be troublesome if treated the same way as comment lines. You can overcome this by using regular expressions. I implore you to research a bit on that.

# If you are running on an online environment such as repl.it, and don't have a sample file at hand, uncomment the following to create a sample file.

##fileHandler2 = open('dev_script.py', 'w+')
##fileHandler2.write('myInt = 4						# an integer variable\n')
##fileHandler2.write('myString = \"Ethan\"					# a string variable\n\n')
##fileHandler2.write('myList = [1, 2, 3, 4, 5, 6]				# a list variable\n')
##fileHandler2.write('mySet = set(myList)			# creating a set from a list\n')
##fileHandler2.write('myTuple = 2, 3				# creating a tuple without parentheses\n')
##fileHandler2.write('myDict = {1: \'one\', 2: \'two\'}		# creating a dictionary variable')
##fileHandler2.seek(0)
##print(fileHandler2.read())