在一个简单的程序中重命名本地绝对文件路径并上传到github,这样没人知道我的目录,这有关系吗?

问题描述

我已经开始使用GitHub来显示一些非常简单的程序。 https://github.com/MasonMcGerry

我想上传更简单的程序,但是其中许多具有我在程序中使用的目录和文件的完整和绝对文件路径。

我不想将其发布到互联网上,因为这似乎并不安全,但这也许无关紧要。

我当时正在考虑对项目进行新的克隆,然后将路径更改为类似/ path / to / this / file的内容。看来这将花费更长的时间,并且调试方面会遇到问题,因为我将使用绝对文件路径在本地主机上进行编程,对其进行测试,然后为“ GitHub ready / safe”版本重写文件路径。 >

我的本​​地主机上的工作版本和“克隆”都将在我的计算机上,但只有“克隆”目录中才会有.git文件

下面的简单程序示例,文件路径已更改。

# for converting tag types to another platforms design

# filepath
fp = '/Users/username/Desktop/Projects/Programs/tagConverter/'

IG = ['IG','ig','instagram','Instagram']
YT = ['YT','yt','youtube','Youtube','Youtube']
end = ['end','END','End','stop','STOP','Stop']
runState = True
# Instagram
def convIG():
    # takes convTxt.txt file,strips out commas and puts each item into array
    # line by line writes into convIG.txt
    # open up file /Users/username/Downloads/tagConverter/convTxt.txt
    # and read it
    f = open(fp + 'convTxt.txt','r')
    inconvIG = f.read()
    outconvIG = '#'+("#").join(inconvIG[0:].split(','))
    g = open(fp + 'convIG.txt','w')
    g.write(outconvIG.replace(' ',''))
    g.close()
    f.close()

# YouTube
"""
will need ML to separate recognizable words with spaces like NLP
other Instagram tags like '#Howto#DIY' will become CSV howto,diy
instead of the preferred 'How to,DIY'
probably will use 'in' operator to help recognize and separate words
i.e.
s = '#howto'
x = ''
if 'how' in s:
    x += 'how '
if 'to' in s:
    x += 'to '
print(x)
x == 'how to '
"""
def convYT():
    # takes convTxt.txt file,strips out # and puts each item into array
    # line by line writes into convYT.txt
    # open up file /Users/username/Downloads/tagConverter/convTxt.txt
    # and read it
    f = open(fp + 'convTxt.txt','r')
    inconvYT = f.read()
    outconvYT = (",").join(inconvYT[0:].split('#'))
    g = open(fp + 'convYT.txt','w')
    g.write(outconvYT)
    g.close()
    f.close()
    
def prompt():
    global runState
    prompt = input("Which Tag Type would you like to convert to?\n")
    if prompt in IG:
        convIG()
    elif prompt in YT:
        convYT()
    elif prompt in end:
        runState = False
    else:
        print('fail')
        
     
# indefinite loop,enter an end word listed in 'end' list
while runState == True:
    prompt()



"""
SOURCES:
https://stackoverflow.com/questions/2783969/compare-string-with-all-values-in-array
https://stackoverflow.com/questions/18132912/checking-if-a-value-is-equal-to-any-value-in-an-array
https://stackoverflow.com/questions/29101836/typeerror-function-object-is-not-subscriptable-python
Reuben Young colleague and friend
https://www.journaldev.com/23763/python-remove-spaces-from-string
https://www.programiz.com/python-programming/global-keyword
https://help.instagram.com/351460621611097

https://www.w3schools.com/python/gloss_python_global_variables.asp
https://beginnersbook.com/2019/03/python-any-function/
https://www.programiz.com/python-programming/list-comprehension

"""

我意识到这段代码很简单,但是随着我的进步,其中一些会变得更加复杂,这会很麻烦。

有没有解决的办法,我是否对这种技术(git / github)抱有偏执或误解/误用了?

解决方法

您确实不想公开本地文件系统。任何硬编码的路径都应该始终是相对的,以避免使潜在的不良行为者了解您的机器组织方式。例外情况是系统默认目录,例如C://Progam Files,其中所有Windows计算机都在其中存储特定类型的数据。

这里最好的解决方案是将这些文件带入项目中某个resources目录下的文件,然后从那里直接访问它们。您还可以解析一个配置文件,该文件将指定在哪里寻找这些文件。例如,

tagConverterPath = "/Users/username/Desktop/Projects/Programs/tagConverter/"

您当然需要将配置文件添加到.gitignore中,并且每个用户都需要定义自己的配置,除非您为其提供了默认配置文件。